PYnative

Python Programming

  • Tutorials
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks

Python JSON

Last updated on December 12, 2020

TweetF  sharein  shareP  Pin

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.

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 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() and json.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 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
  • Load JSON into an OrderedDict
  • How to handle numeric JSON data using parse_float and parse_int kwarg in json.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 →

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.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

Is this article/website helpful?

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Show All Exercises

 Show All Quizzes  

Keep Reading Python

Python Input & Output Python MySQL Python PostgreSQL Python SQLite Python JSON Python Quizzes Python Exercises Python Generate random data

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your code </pre>

Leave a Comment

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your code </pre>

Practice Python


Practice Python using our 15+ Free Topic-specific Exercises and Quizzes

All exercises and Quizzes are tested on Python 3

Exercises
Quizzes

 Python JSON

  • Python JSON Guide
  • JSON Serialization
  • JSON Parsing
  • JSON Validation
  • PrettyPrint JSON
  • Make Python Class JSON serializable
  • Convert JSON Into Custom Python Object
  • Python JSON Unicode
  • Parse JSON using Python requests
  • Post JSON using Python requests
  • Serialize DateTime into JSON
  • Serialize Python Set into JSON
  • Serialize NumPy array into JSON
  • Parse multiple JSON objects from file
  • Parse Nested JSON
  • Python JSON Exercise
TweetF  sharein  shareP  Pin

About PYnative

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

Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow PYnative

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter   Facebook
  • RSS | Sitemap

Legal Stuff

  • About Us
  • Privacy Policy
  • Cookie Policy
  • Terms Of Use
  • Contact Us
DMCA.com Protection Status

Copyright © 2018-2021 · [pynative.com]

This website uses cookies to ensure you get the best experience on our website.Privacy PolicyGot it!