json





Hello Folks...!!!

Today Let us look at the JSON  package in Python.

What is JSON ?

  • The full form of JSON is JavaScript Object Notation. 
  • It is a format for storing and exchanging data.
  • It is mostly used when data is transferred between server and web pages.
  • These look similar to nested dictionaries in Python. They are just key-value pairs.
  • Look at the image below to visualize a JSON file with two records.
  • The objects of JSON are called JSON string.




Python provides a package for creating and manipulating JSON files.

To utilize the functions provided by this package, import it by writing : import json

Serialization and deserialization

Serialization is the conversion of a python object (mostly dictionaries) into JSON String. Deserialization refers to vice-versa. To be precise it the former refers to conversion of Python objects into JSON format and the later refers to vice-versa.

json.load( )

  • It is used to read the JSON file directly.
  • It can deserialize itself. 
  • The parameter required is the path of the json file.
  • You can directly print the content of a json file as the pointer to the json file is the parameter passed.
For example:



Here I have taken a file named test2.json. I have opened it in read mode and passed to the load() function. We can directly print the contents of the function here. The return type of the function is a Python object- Dictionary. Then the contents can be treated as nor dictionary contents and manipulations can be made using the methods available for dictionary. Look at the example below.



json.loads( )

  • Here the 's' refers to string. 
  • It expects a JSON string parameter instead of file pointer. 
  • It cannot deserialize itself.
  • You have to read the content and use this method for parsing the contents.
For example


You will get an error, if you pass the file to loads( ) function. To overcome this, we have to read the contents first and then pass it to the loads(). So we can write as given below.



This also returns a dictionary data type. But the only difference is, you have to read the content first and pass it as a string to the loads() function.

json.dump( ):

  • Converts Python objects (lists, dictionaries,etc,.)  into appropriate json objects.
  • This method is appropriate when Python objects are required to be stored in a file.
  • The syntax is as given below, (d-object to be dumped, fp- pointer to file).
    json.dump(d,fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None)
Let me explain the parameters now.
  1. skipkeys : If the key is not of standard allowed types like int, float, string, None or bool, error will be generated while dumping them, to avoid that this parameter is set to true.
  2. ensure_ascii : If it is not set to true the non-ASCII characters are dumped into the output file as it is. By default the value is true.
  3. check_circular : To avoid circular reference check for containers , it is set to False. But this will lead to overflow and thus the default value is True. Whenever a Python object is created, the memory manager keeps counting the number of references to the object. Once the reference count becomes zero, the object is destroyed. This is what we call as automatic garbage collection. But a situation where one object - for say A refers to B and in parallel B refers to A, then it is a loop and garbage collection never happens. This is called as cyclic reference.
  4. allow_nan : It helps to convert float values into desirable JSON objects.
  5. cls : Used for setting custom decoder. The default one used is JSONDecoder.
  6. indent : This is for formatting the content in file. If you simply give double quotes(""), each key-value pair in the dictionary will be added in a new line in the file.
  7. seperators : This parameter takes up either one or two values. The first value specifies the symbol that separates one key-value pair from another. The next one specifies the symbol that separates the value from its key. It has to be mentioned in the format (item_separator, key_separator). For example (';', ':') - Now between each key and value you will find a colon (:) and between each key-value pair you will find semicolon (;) ie., A:B; C:D; E:F;
Check out my article in geeksforgeeks for more deeper knowledge on dump().

json.dumps()

  • Similar to dump() method.
  • It returns a String representing the JSON object for the input Python object passed.
  • It takes the Python object one of the parameters and the file pointer is not required.
  • Rest are same as the parameters for dump() method.
Look at the example.




The output will be as follows:



There are flaws with using this dump methods. As per the official docs of json package: 
  • Unlike pickle and marshal packages, JSON is not a framed protocol, so trying to serialize multiple objects with repeated calls to dump() using the same fp will result in an invalid JSON file.
  • Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings. As a result of this, if a dictionary is converted into JSON and then back into a dictionary, the dictionary may not equal the original one. That is, loads(dumps(x)) != x if x has non-string keys.
Thus one must be very careful while using these methods.

Next TopicπŸ‘‰

Comments