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
You can also search for these topics, python json read from file, json bytes in python, python json contains keys, example for python json, key values in json python, working of functions in python json, python generator and key check with json, how to install the json in python, json in python install, json request in python, how to create json in python, example for python in json, python json error and format, get the python value of 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 :-

30

You can also search for these topics, python parse json convert, how to convert python to python, json.loads() function in python, parse json to convert form json python.

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 :-

{"name": "John", "age": 30, "city": "New York"}

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 :-

{"name": "John", "age": 30}
["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 :-

{"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann","Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}

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"
    ]
}

You can also search for these topics, python format the result code, python format the result decimal places, python Format the empty Result, how to print the result format, check the keyerror to format the result, python format the result method and number, Example for the python result in format.

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"
}

You can also search for these topics, python order the result by list, how to order the python code, check the containing order of the result, import the order of result, index of the order the python result, python result the order number, how to reverse the ordered the result, ways to order the result value in python.