In this tutorial, we’ll see how we can create, manipulate, and parse JSON in Python using the standard a json
module. The built-in Python json module provides us with methods and classes that are used to parse and manipulate JSON in Python.
What is JSON
JSON (an acronym for JavaScript Object Notation) is a data-interchange format and is most commonly used for client-server communication. Refer Official JSON documentation
Example:
{"name": "jane doe", "salary": 9000, "email": "JaneDoe@pynative.com"}
A JSON is an unordered collection of key and value pairs, resembling Python’s native dictionary.
- Keys are unique Strings that cannot be null.
- Values can be anything from a String, Boolean, Number, list, or even null.
- A JSONO can be represented by a String enclosed within curly braces with keys and values separated by a colon, and pairs separated by a comma
Whenever the client needs information, it calls the server using a URI, and the server returns data to the client in the form of JSON. Later we can use this data in our application as per our requirement. Also, when the client application wants to store the data on the server. It can POST that data in the form of JSON.
JSON is most commonly used for client-server communication because:
- It is human readable.
- It’s both easy to read/write and
- JSON is language-independent.
Python built-in module json
Python comes with a built-in module called json for working with JSON data. You only need to add import json
at the start of your file and you are ready to use it.
This Python JSON tutorial contains the following articles that cover the sub-topic and frequently asked questions in detail.
Python JSON Encoding or Serialization →
When we encode Python Objects into JSON we call it a Serialization. In this section, we will cover the following.
- The mapping between JSON and Python entities while encoding
json.dumps
to encode JSON Data into native Python String.json.dump
to encode and store JSON Data into a file- Understand the various use of of
json.dump()
andjson.dumps()
method in detail - Learn how to skip nonbasic types while encoding Python Objects into JSON
- Write Indented and pretty printed JSON data into the file
- Compact JSON encoding to save file space by changing JSON key-value separator using Python
- Encode Unicode data as-is into JSON
Python JSON Parsing/Deserialization/Decoding →
When we convert JSON encoded/formatted data into Python Types we call it a JSON deserialization or parsing. In this section, we will cover the following.
- The Mapping between JSON and Python entities while decoding
- How to read JSON data from a file using
json.load()
and convert it into Pythondict
so we can access JSON data in our system. - Convert JSON response or JSON string to Python dictionary using
json.loads()
- Understand the various use of
load
andloads()
method in detail - Learn how to parse and Retrieve nested JSON array key-values
- Load JSON into an OrderedDict
- How to handle numeric JSON data using
parse_float
andparse_int
kwarg
injson.load()
- Implement a custom JSON decoder
Validate JSON Data using Python →
There are multiple scenarios where we need various types of JSON validations. In this section, we will cover the following.
- Check if a JSON document is valid JSON as per the JSON specification
- Validate JSON Object from the command line
- Validate JSON File
- Validate JSON Schema by checking all necessary fields present in JSON and also validate data types of those fields
PrettyPrint JSON Data using Python →
PrettyPrint means JSON data should be correctly indented and easy-to-read format. In this section, we will cover the following.
- Write Indented and Pretty-printed JSON data into a file
- Read and PrettyPrint JSON file using Python
- Pretty-print JSON from the command line
- Sort JSON keys alphabetically
- Change JSON key and value separator
Make a Python Class JSON serializable (Custom JSON Encoder) →
The Python built-in json module can only handle Python primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, Numbers, None, etc.). So when we try to serialize custom class instance into JSON, we receive a type error. The Class is not JSON serializable. In this section, I will show you how to convert any arbitrary Python objects to JSON.
- Learn the different ways to write custom JSON Encoder to convert custom Class Object into JSON
- Learn how to override the default behavior or Python JSON encoder.
Convert JSON Data Into a Custom Python Object (Custom JSON Decoder) →
- Learn how to convert JSON data into a custom Python object instead of a dictionary.
- Understandearn the different ways to write custom JSON Decoder to convert custom JSON Decoder.
- Learn how to override the default behavior or Python JSON decoder.
Python JSON Handle Unicode Data →
- JSON serialize Unicode or non-ASCII data as-is. instead of
\u
escape sequence (Example, Store Unicode stringø
as-is instead of\u00f8
in JSON) - JSON serialize all non-ASCII characters escaped (Example, Store Unicode string
ø
as\u00f8
in JSON)
Parse a JSON response using the Python requests library→
In this section, we will learn how to send a RESTful GET call to a server, and Parse a JSON response using the requests library.
Post JSON using the requests library →
Learn how to post a JSON from a client to a server using a requests library
Serialize Python DateTime into JSON →
The Python built-in json module can only handle Python primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, Numbers, None, etc.). So when we try to serialize Python Object which contains DateTime instance into JSON, we receive a type error. The DateTime is not JSON serializable. In this section, we will see serialize and deserialize DateTime instance to and from JSON.
Serialize Python Set into JSON →
In this section, we will see how to JSON serialize Python set. To solve TypeError: Object of type set is not JSON serializable
we need to build a custom encoder to make set JSON serializable.
Serialize NumPy array into JSON →
In this section, we will see how to JSON serialize NumPy ndarray
Python Check if a key exists in JSON →
In this section, instead of iterating entire JSON, we will see:
- How to Check if the key exists or not in JSON. Check if there is a value for a key in JSON
- Return default value if the key is missing
- Find if the nested key exists in JSON and Access nested key directly
Parse multiple JSON objects from file →
In this section, we will see how to solve a json.decoder.JSONDecodeError: Extra dat
error so that we can load and parse a JSON file with multiple JSON objects.
Parse JSON serialized list which contains a nested dictionary →
This Python JSON exercise is to help Python developer to learn and practice JSON creation, manipulation, Parsing. Each question includes a specific JSON topic you need to learn. When you complete each question, you get more familiar with JSON encoding and decoding in Python.