![]() |
VOOZH | about |
JSON (JavaScript Object Notation) is a text format used to store data in key–value pairs inside curly braces, similar to a Python dictionary.
Output:
{"age": 31, "Salary": 25000, "name": "John"}
As you can see, JSON supports primitive types like strings and numbers, as well as nested lists (arrays) and objects
Output:
["Welcome", "to", "GeeksforGeeks"]
["Welcome", "to", "GeeksforGeeks"]
"Hi"
123
23.572
true
false
null
Serialization is the process of converting data into a format that can be stored or transmitted. In the context of JSON, it means converting Python objects into JSON format. In Python, this is done using the json module’s dump() function. It converts Python data (like dictionaries or lists) into JSON and writes it to a file.
Refer to the table below for a clearer understanding of how Python objects are converted into JSON format.
| Python object | JSON object |
|---|---|
| dict | object |
| list, tuple | array |
| str | string |
| int, float | numbers |
| True | true |
| False | false |
| None | null |
Example: Consider the given example of a Python object.
Deserialization is the reverse of serialization. It converts JSON data back into its corresponding Python objects. In Python, this is done using the json module’s load() or loads() method.
Example: Here we created a JSON object in python and then used .loads method to Deserialize it.
Encoding and decoding refer to the process of converting data between Python objects and JSON format. Encoding transforms Python data into JSON so it can be stored or transmitted, while decoding converts JSON data back into usable Python objects.The key concepts involved in this process are outlined below.
Run the command below in your terminal to install demjson
pip install demjson
Encoding: Encoding in Python JSON is done using json.dumps() (or json.dump() for files), which converts Python objects into JSON format.
Example 1: Encoding using demjson package.
Output:
[{"Chemistry":70, "Math":50, "physics":60}]
Decoding: The decode() function is used to convert the JSON object into python format type.
Example 2: Decoding using demjson package
Output:
{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
Example 3: Encoding and Decoding using dumps() and loads().
The JSON library can also be used from the command line, to validate and pretty print your JSON.
Example: In this command, echo prints a JSON string directly in the terminal. This JSON data can then be passed to a JSON tool to check whether the format is valid and to display it in a clean, structured way for better readability.
echo '{ "name": "Monty", "age": 45 }'
Output:
{ "name": "Monty", "age": 45 }
First, install jmespath:
pip install jmespath
After installation, you can import and use it in Python using:
>>> import jmespath
>>> j = {"people": [{"name": "Abhi", "age": 22}]}
>>> jmespath.search("people[*].age", j)
[22]
For practice, we use JSONPlaceholder, a free API that provides sample JSON data for learning and testing. We will use the requests library to fetch data from this API and process the JSON response in Python. The following script demonstrates how to fetch JSON data from an API, filter it and store the result in a file.
Output:
In the output, a new file named sample.json is created. This file contains only the filtered records that match the given conditions. The data is stored in proper JSON format and is neatly structured because of indent=2