JSON



What is JSON?

JavaScript Object Notation (JSON) is a lightweight data-interchange format based on the syntax of JavaScript objects. It is a text-based, human-readable, language-independent format for representing structured object data for easy transmission or saving. JSON objects can also be stored in files — typically a text file with a .json extension and a application/json MIME type. Commonly, JSON is used for two way data transmission between a web-server and a client in a REST API.


Despite the fact that it’s syntax closely resembles JavaScript objects, JSON can be used independently outside JavaScript. In fact, a majority of programming languages have libraries to manipulate JSON. In this article, our focus will be on manipulating JSON data in python, using the built-in json module.


And some basic terminology …

  • JSON exists as a string — a sequence (or series) of bytes. To convert a complex object (say a dictionary) in to a JSON representation, the object needs to be encoded as a “series of bytes”, for easy transmission or streaming — a process known as serialization.

  • Deserialization is the reverse of serialization. It involves decoding data received in JSON format as native data types, that can be manipulated further.


Why JSON?

  • Compared to its predecessor in server-client communication, XML, JSON is much smaller, translating into faster data transfers, and better experiences.

  • JSON exists as a “sequence of bytes” which is very useful in the case we need to transmit (stream) data over a network.

  • JSON is also extremely human-friendly since it is textual, and simultaneously machine-friendly.

  • JSON has expressive syntax for representing arrays, objects, numbers and booleans.


Working with Simple Built-in Datatypes

Generally, the json module encodes Python objects as JSON strings implemented by the json.JSONEncoder class, and decodes JSON strings into Python objects using the json.JSONDecoder class.


Serializing Simple Built-in Datatypes

By default, the JSON encoder only understands native Python data types (str, int, float, bool, list, tuple, and dict). The json module provides two very handy methods for serialization based on the conversion table below:

Python — JSON conversion

  • dumps() — to serialize an object to a JSON formatted string.

  • dump() — to serialize an object to a JSON formatted stream ( which supports writing to a file).

Lets look at an example of how to use json.dumps() to serialize built in data types.