Python Data Persistence – json Module

Python Data Persistence – json Module

JavaScript Object Notation (JSON) is an easy-to-use lightweight data-interchange format. It is a language-independent text format, supported by many programming languages. This format is used for data exchange between the web server and clients. Python’s j son module, being a part of Python’s standard distribution, provides serialization functionality similar to the pickle module.

Syntactically speaking, json module offers identical functions for serialization and de-serialization of Python objects. Module-level functions dumps () and loads () convert Python data to its serialized string representation and vice versa. The dump () function uses a file object to persistently store serialized objects, whereas load () function reconstructs original data from the file.
Notably, dumps () function uses additional argument sort_keys. Its default value is False, but if set to True, JSON representation of Python’s dictionary object holds keys in a sorted order.

Example

>>> import json
>>> data=[{'product':'TV1, 1 brand':'Sam¬sung ', 'price' : 25000 }, {1 product' : 'Computer', 'br
and':'Dell','price':40000},{'product':'Mo¬bile ', 'brand' :'Redmi', 'price' : 15000} ]
>>> JString=json.dumps(data, sort_keys=True) .
>>> JString
'[{"brand": "Samsung", "price": 25000, "product": "TV"}, {"brand": "Dell", "price": 40000, "product": "Computer"}, {"brand": "Redmi", "price": 15000, "product": "Mobile"}]'

The loads () function retrieves data in the original format.

Example

>>> string=json.loads(JString)
>>> string
[{'brand': 'Samsung', 'price': 25000, 'product':
'TV'}, {'brand': 'Dell', 'price': 40000, 'product': 'Computer'}, {'brand': 'Redmi', 'price': 15000,
'product': 'Mobile'}]

To store/retrieve JSONed data to/from a disk file, use dump () and load () functions respectively.

Example

>>> file=open ( ' j son. txt' , ' w' )
>>> j son. dump (data, file)
>>> file=open ( ' j son. txt' , ' r ' )
>>> data=j son. load (file)

Python’s built-in types are easily serialized in JSON format. Conversion is done, as per the corresponding table below:ftaZ>/e 6.1)

Python JSON
Dict Object
List , tuple Array
Str String
Int , float , int-& float-derived Enums Number
True True
False False
None Null

However, converting objects of a custom class is a little tricky. The json module defines JSONEndoder and JSONDecoder classes. We need to subclass them to perform encoding and decoding of objects of user-defined classes.

The following script defines a User class and an encoder class inherited from the JSONEncoder class. This subclass overrides the abstract default () method to return a serializable version of the User class which can further be encoded.

Example

import json
class User:
      def__init__(self,name, email, pw):
             self.name=name
             self.email=email
             self.pw=pw
     def__str__(self):
return ('Name: {} email: {} password: {}'. \ format(self.name, self.email, self.pw)
class UserEncoder(json.JSONEncoder):
   def default(self, z) :
       if isinstance(z, User):
           return (z.name, z.email, z.pw)
     else:
           super().default(self, z)
usdr1=User('Raj an','R@a.com','**')
encoder=UserEncoder( )
obj =encoder.encode(user1)
file=open (' j sonOO. txt' , ' w ' )
json.dump (obj , file)
file. close ()

To obtain the original object, we need to use a subclass of JSONDecoder. The subclass should have a method that is assigned a value of the object_hook parameter. This method will be internally called when an object is sought to be decoded.

Example

import j son
class UserDecoder(json.JSONDecoder):
         def__init__(self):
              j son.JSONDecoder.__init__
(self,object_hook=self.hook)
         def hook(self,obj):
               return diet(obj)
decoder=UserDecoder()
file=open (' j sonclass . txt' , ' r' )
retobj =j son. load (file)
print (decoder.decode(retobj))

The j son. tool module also has a command-line interface that validates data in string or file and delivers nice formatted output of JSON objects.
Assuming that the current working directory has ‘file.txt’ that contains text in JSON format, as below:

Example

{"name": "Rajan", "email": "r@a.com", "pw": "**"}

The following command produces pretty print output of the above string.

E: \python37>python -m j son. tool file.txt
{
"name": "Rajan",
"email": "rxa.com",

It accepts a -sort-keys command line option to display keys in ascending order.

E:\python3 7>python -m json.tool file.txt --sort-keys
{
"email": " rwa.com",
"name": "Raj an",
"pw" : " * * "
}