PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » JSON

Python JSON

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 json Module

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.

Python JSON Tutorial
Python JSON Tutorial

This Python JSON tutorial contains the following articles that cover the sub-topic and frequently asked questions in detail.

Python JSON Encoding

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 write JSON into a file
  • Understand the various use of json.dump() and json.dumps() method in detail
  • Write Indented and pretty printed JSON data into the file
  • Compact JSON encoding
  • Encode Unicode data as-is into JSON

Python JSON Parsing

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 Python dict 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 and loads() method in detail
  • Learn how to parse and Retrieve nested JSON array key-values
  • 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

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 encode

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

Python JSON Exercise

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.

All Python JSON Tutorials: -

Python JSON Exercise

Updated on: December 8, 2021 | 6 Comments

Filed Under: Python, Python Exercises, Python JSON

Python JSON Serialize Set

Updated on: May 14, 2021 | Leave a Comment

Filed Under: Python, Python JSON

Python Parse multiple JSON objects from file

Updated on: May 14, 2021 | 21 Comments

Filed Under: Python, Python JSON

Python Check if key exists in JSON and iterate the JSON array

Updated on: May 14, 2021 | 16 Comments

Filed Under: Python, Python JSON

Python Serialize NumPy ndarray into JSON

Updated on: May 14, 2021 | 1 Comment

Filed Under: Python, Python JSON

Validate JSON data using Python

Updated on: May 14, 2021 | 5 Comments

Filed Under: Python, Python JSON

Python Encode Unicode and non-ASCII characters as-is into JSON

Updated on: May 14, 2021 | Leave a Comment

Filed Under: Python, Python JSON

Python Convert JSON data Into a Custom Python Object

Updated on: May 14, 2021 | 4 Comments

Filed Under: Python, Python JSON

Python Post JSON using requests library

Updated on: May 14, 2021 | 3 Comments

Filed Under: Python, Python JSON

Python Serialize Datetime into JSON

Updated on: May 14, 2021 | 1 Comment

Filed Under: Python, Python JSON

Make a Python Class JSON Serializable

Updated on: May 14, 2021 | 12 Comments

Filed Under: Python, Python JSON

Converting JSON String to Dictionary Not List

Updated on: May 14, 2021 | 1 Comment

Filed Under: Python, Python JSON

Parse a JSON response using Python requests library

Updated on: May 14, 2021 | 3 Comments

Filed Under: Python, Python JSON

Python JSON Parsing using json.load() and loads()

Updated on: May 14, 2021 | 12 Comments

Filed Under: Python, Python JSON

Python JSON dump() and dumps() for JSON Encoding

Updated on: May 14, 2021 | 13 Comments

Filed Under: Python, Python JSON

Python PrettyPrint JSON Data

Updated on: May 14, 2021 | Leave a Comment

Filed Under: Python, Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com