What is JSON?
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.
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.