JSON is a syntax for storing and exchanging data, which is text, written with JavaScript object notation
Python has a built-in package called json
, which can be used to work with JSON data
json module can be imported as :
import json
A JSON string can be parsed by using the json.loads()
method
This returns a Python dictionary
import json
# some JSON
x = '{ "name":"Johnson", "age":33, "city":"New Jersey"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y)
Output :
{'name': 'Johnson', 'age': 33, 'city': 'New Jersey'}
A Python object can be converted into a JSON string by using the json.dumps()
method
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)
Following types of Python objects can be converted into JSON strings
Python objects are converted into their JSON (JavaScript) equivalent as follows:
Python | JSON |
---|---|
dict | Object |
list | Array |
tuple | Array |
str | String |
int | Number |
float | Number |
True | true |
False | false |
None | null |
Both lists and tuples are converted to arrays
While converting JSON to Python, arrays are converted to lists
import json
>>> print(json.dumps({"name": "Johnson", "age": 33}))
{"name": "Johnson", "age": 33}
>>> print(json.dumps(["list", "values"]))
["list", "values"]
>>> print(json.dumps(("tuple", "values")))
["tuple", "values"]
>>> print(json.dumps("string"))
"string"
>>> print(json.dumps(42))
42
>>> print(json.dumps(31.76))
31.76
>>> print(json.dumps(True))
true
>>> print(json.dumps(False))
false
>>> print(json.dumps(None))
null
Consider following example
import json
# Python dict
x = {"name": "Johnson", "age": 33, "married": True, "divorced": False, "languages": ("Python", "Javascript"), "pets": None,
"children": [ {"name": "Anna", "age": 15}, {"name": "Bil", "age": 11} ]
}
# dump json
print(json.dumps(x))
Output
{"name": "Johnson", "age": 33, "married": true, "divorced": false, "languages": ["Python", "Javascript"], "pets": null, "children": [{"name": "Anna", "age": 15}, {"name": "Bil", "age": 11}]}
Above example prints a JSON string, but it does not have indentations and line breaks
The json.dumps()
method has parameters to include indentation and separators, to increase readability
The indent
parameter can be used to define the numbers of indents:
indented = json.dumps(x, indent=4)
print(indented)
Output
{
"name": "Johnson",
"age": 33,
"married": true,
"divorced": false,
"languages": [
"Python",
"Javascript"
],
"pets": null,
"children": [
{
"name": "Anna",
"age": 15
},
{
"name": "Bil",
"age": 11
}
]
}
Separators can also be defined, whose default value is (", ", ": "), which means it uses a comma and a space to separate each object, and a colon and a space to separate keys from values
The separators
parameter can be used to change the default separator
custom = json.dumps(x, indent=4, separators=(". ", " = "))
print(custom)
Output
{
"name" = "Johnson".
"age" = 33.
"married" = true.
"divorced" = false.
"languages" = [
"Python".
"Javascript"
].
"pets" = null.
"children" = [
{
"name" = "Anna".
"age" = 15
}.
{
"name" = "Bil".
"age" = 11
}
]
}
json.dumps()
method accepts sort_keys
parameter to sort the keys in the resultant string
sorted = json.dumps(x, indent=4, sort_keys=True)
print(sorted)
Output
{
"age": 33,
"children": [
{
"age": 15,
"name": "Anna"
},
{
"age": 11,
"name": "Bil"
}
],
"divorced": false,
"languages": [
"Python",
"Javascript"
],
"married": true,
"name": "Johnson",
"pets": null
}
The keys of objects are sorted