Python JSON
JSON is a storage and data exchange syntax.
JSON is the text of the notation of the object JavaScript.
Python provides an integrated json package to utilise for JSON data processing.
Example :- Import the json module :
import json
Parse JSON - Convert from JSON to Python
You can use the json.loads() method to parse a JSON string.
Example :- Convert from JSON to Python :
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"])
Output :-
Related Links
Convert from Python to JSON
You can turn it into a JSON string by using the json.dumps() method if you have a Python object.
Example :- Convert from Python to JSON :
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)
Output :-
Python objects of the following types can be converted to JSON strings :
- dict
- list
- tuple
- string
- int
- float
- True
- False
- None
Example :- Convert Python objects and display the values to JSON strings :
import json
print(json.dumps({"name": "John", "age": 30}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
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))
Output :-
["apple", "bananas"]
["apple", "bananas"]
"hello"
42
31.76
true
false
null
Python's objects will be transformed from Python to JSON into the JavaScript equivalent :
| Python | JSON |
|---|---|
| dict | Object |
| list | Array |
| tuple | Array |
| str | String |
| int | Number |
| float | Number |
| True | true |
| False | false |
| None | null |
Example :- Convert the object Python that contains all forms of legal data :
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}
]
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
Output :-
Format the Result
The sample above displays a JSON string, but without indentation and line breaking is not easily readable.
The method json.dumps() offers options to facilitate the reading of the following :
Example 1 :- To define the indent number, use the indent parameter :
import json
x = {
"name": "John",
"age": 30,
"children": ("Ann","Billy")
}
# use four indents to make it easier to read the result:
print(json.dumps(x, indent=4))
Output :-
{
"name": "John",
"age": 30,
"married": true,
"divorced": false,
"children": [
"Ann",
"Billy"
]
}
The default value (", ":") can alternatively be defined using a comma and an area for separating each object, a column and a space for separating keys from values, etc.
Example 2 :- To alter the default separator, use the separators parameter :
import json
x = {
"name": "John",
"age": 30,
"children": ("Ann","Billy")
}
# use . and a space to separate objects, and a space, a = and a space to separate keys from their values:
print(json.dumps(x, indent=4, separators=(". ", " = ")))
Output :-
{
"name" = "John".
"age" = 30.
"children" = [
"Ann".
"Billy"
]
}
Related Links
Order the Result
The function json.dumps() has options to organize the keys :
Example :- To specify whether or not the result should be sorted, use sort_keys parameter :
import json
x = {
"name": "John",
"age": 30,
"children": ("Ann","Billy")
}
# sort the result alphabetically by keys:
print(json.dumps(x, indent=4, sort_keys=True))
Output :-
{
"age": 30,
"children": [
"Ann",
"Billy"
],
"name": "John"
}