Python JSON
In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the data types it supports, parsing of JSON string into a python object and python object into JSON string, etc.
JSON
It is a condensation used for JavaScript object notation. It is a typescript written in JavaScript. It is a format used to store the data. It is also used for data formatting, just like XML. It is more versatile than XML.
Python language has got an innate package known as JSON.
To be able to use JSON in python, it needs to be imported first. The following way is used to import the JSON module–
import json
Features of JSON:
- It is easy to comprehend and write.
- It weighs less and is based on text. It is comparatively easier than XML.
- It is extensively used to store data and as a communication format on the web.
- Despite being derived from JavaScript, it is Language independent. This means the code written in any other programming language can be generated and parsed in JSON data.
Syntax Guidelines of JSON:
1. Data should be written in name/value pair
For example:
{“name”: “Javatpoint”}
{“exam”: “civil service”}
2. Comma acts as a data separator.
For example:
{“name”: “Javatpoint”, exam: “civil service”}
{“name”: “laptop”, company: “apple”}
3. Curly braces act as an object holder
For example:
{“name”: “laptop”, company: “apple”}
4. Square brackets act as an array holder.
val person1 = {“name”: “Nick”, “place”: “Mussorie”, Occupation”: [“Writer”, “Singer”, “dancer”]}
Data types supported in JSON
JSON supports the following six data types.
1. String
It is a primitive data type.
The string must be written within a double quotation.
Syntax:
{“name”: “Ravi”}
{“Designation”: “IAS”}
{“cadre”: “Bihar”}
2. Number
It is also a primitive data type.
It is represented in decimal and octal. It does not support hexadecimal.
Syntax:
{“salary”: 5000000}
{“age”: 40}
3. Null
It is a primitive data type.
It is a special type of value that can be assigned to any other data type, including numbers, strings, arrays, or Boolean.
Syntax:
{
“outcome”: true,
“marks”: null,
“registration” : 28
}
4. Boolean - primitives data types
This can either be true or false.
Syntax:
{“outcome”: true}
5. Object –
It is a complex data type.
It is a set of name or value pairs written within a curly bracket.
Syntax:
{key1: value1, key2: value2….}
6. Array
It is a complex data type.
It is an ordered collection of similar data enclosed in a square bracket.
Syntax:
[value1, value2 ,…..}
Example: This figure demonstrates the document written in JSON format.

Parsing of JSON
Parsing of JSON means converting the JSON string into an equivalent python object and also a python object into an equivalent JSON string.
Conversion from JSON to Python
Python offers a json.loads() method to convert a JSON string into a python object.
Example: In the below code, the use of json.loads() method in parsing is being demonstrated. python-json

Output:

Conversion from Python to JSON
Method json.dumps() is used to convert python objects into JSON strings.
It can convert objects of various data types such as a dictionary, list, tuple, string, float, int, etc.
Example: In this figure, the usage of json.dumps() method is being demonstrated.

Output: The following figure shows the result after the python object gets converted into a JSON string with the aid of json.dumps() method.

Here is an illustration to show the conversion of a python object into all legal data types.
Example: The following figure shows the data types- string, int, Boolean, and array getting converted into JSON format.

Output: The figure below shows an output after python objects get converted into JSON strings. But the problem here is its readability. The data being shown is not easily understandable.

Example: The following code is written to remove the above readability issue by using proper indentations and separators.

Explanation: The above code intends to provide readable data after getting converted into an equivalent JSON format.
Output: The below figure shows an output after a python object gets converted into a JSON document but in a proper and enhanced readable format.
