The Python JSON
This is the
python json to string part of our tutorial. And in this programming language, JSON is basically nothing but a kind of syntax for exchanging and storing all kinds of data. It is important for you to know that JSON is a text that is written with the help of JavaScript object notation.
The JSON in Python
The entire package of JSON comes built-in in this programming language. This particular built-in package can also be used in this programming language to work with particular JSON dates. For example, if you wish to import any JSON module then
import json
The Conversion from JSON to Python - Parse JSON
If you wish to parse any
python parse json string that you already have then you can do that with the help of the
json.loads ( ) method. However, the result that you acquire after going through this particular step will be in the Python dictionary. Now, if you wish to convert JSON to Python then you can do that by following the below-mentioned method.
import json
# some JSON :
x = ‘ { “name”: “John”, “age”: 30, “city”: “New York”} ’
# parse x :
y = json.loads ( x )
# the result is a python dictionary :
print (y [“age”] )
To Convert from Python to JSON
If you wish to convert a Python object that you already possess into a JSON string then you can also do that. And to perform this
python write json to file conversion you will be required to use the
json.dumps ( ) method. For example
import json
# a Python object ( dict ) :
x = {
“name” : “John”,
“age” : 30,
“city” : “New York”
}
# convert into JSON :
y = json.dumps ( x )
# the result is a JSON string :
print ( y )
It is also important for you to know that you can convert a number of different Python object into a particular kind of
python json decode string. The number of different python objects that can be converted into JSON strings is mentioned below.
- None
- False
- True
- Float
- Int
- String
- Tuple
- List
- Dict
For example, if you to change a particular python object into JSON string and then print the values then
import json
print (json.dumps ( { “name”: “John”, “age” : 30} ) )
print (json.dumps ( [ “apple”, “banana” ] ) )
print (json.dumps ( ( “apple”, “banana”) ) )
print (json.dumps ( “hello” ) )
print (json.dumps (42 ) )
print (json.dumps ( 31.76 ) )
print (json.dumps (True) )
print (json.dumps (False) )
print (json.dumps (None) )
This above example will help you in achieving the task of
python write json to file.
As a developer who is using this particular programming language, you should also be aware of the fact that whenever you convert Python objects to JSON then those objects are also converted to JSON or JavaScript equivalent. For your ease of learning, we have formulated a particular table that contains information regarding the JavaScript equivalent that Python objects get converted to. The table of
Python json get value is mentioned below.
Python Objects |
JSON (JavaScript) Equivalents |
Dict |
Object |
List |
Array |
Tuple |
Array |
Str |
String |
Int |
Number |
Float |
Number |
True |
True |
False |
False |
None |
Null |
For example, if you wish to convert a particular type of
Python json decode object that contains all kinds of legal data type then
import json
x = {
“name” : “John”,
“age” : 30,
“married” : True,
“divorced” : False
“children” : (“Ann”, “Billy”) ,
“pets” : None,
“cars” : [
{“model”: “BMW 230”, “mpg”: 27.5 } ,
{“model” : “Ford Edge”, “mpg” : 24.1 }
]
}
print (json.dumps ( x ) )
Format the Result
The example that we have mentioned above regarding the printing of the
python parse json string is quite hard to read since it does not have any line breaks or indentations. However, the
json.dumps ( ) method has a number of different parameters that can make its results a little easier to read. For example, if you wish to define the particular number of indents by using the
indent parameter then you can do that by following the below-mentioned method. This will help you in accomplishing the
python json get value task.
json.dumps ( x, indent = 4 )
While using this programming language, if you wish to define the different separators then you can also do that by using the various different values like “,”, “:,”. This basically means that you can use a comma and a space to separate the different objects. You can also use a space to separate the different keys from the values. For example, if you wish to change the default separator in
python json to string then you can do that by using the separator parameter then
json.dumps ( x, indent=4, separators=( “. “, “ = “) )
To Order the Results
You can also use the parameters of the above-mentioned method to order the different keys from the result. For example, if you wish to specify whether you want the results to be sorted or not by using the
sort_keys then
json.dumps ( x, indent=4, sort_keys=True )
With this, we finish the
Python parse json part of our entire python tutorial.