Reading and Writing of JSON Data in Python

Keywords: Python JSON encoding Javascript

Preface

  • JSON (JavaScript Object Notation, JavaScript Object Representation) is a lightweight data exchange language

  • JSON is a language-independent text format, and JSON data format is language-independent.

  • The characteristics of JSON data format:
    • Objects are represented as key-value pairs
    • Data is separated by commas
    • Curly brackets save objects
      • An object contains a series of unordered name/value pairs, and an object begins with {and ends with}. Use between each name/value pair: split
    • Square brackets hold arrays.
      • An array is a collection of values, and an array begins and ends with. Use and partition between group members

JSON Data Reading

First, we need to import modules

 import json

Reading json data, json module provides two methods, loads and load, to achieve "deserialization". So, there are differences between them. The comparison is as follows:

 def load(fp, *, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
        
  """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
    a JSON document) to a Python object.""" 
def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
        parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
        
    """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
    containing a JSON document) to a Python object."""

It's easy to see that load needs to pass in a file object, while loads needs a memory object.

Now there is a document. data.json The data are as follows:

  {
    "id": "2016984", 
    "title": "Heading 2", 
    "publish_data": "2019/4/15", 
    "publish_place": "Guangzhou", 
    "software": "boss",
    "skill": ["Go", "Ruby", "Java", "Python"]
}

Now use load and loads to read and print out the contents of data.json, respectively.

    with open('datas.json', 'r', encoding='utf-8') as fo:
        d= json.load(fo)
    print(d, type(d))
        
    print('-'*150) # Divide it up to make a good distinction.
    
    with open('datas.json', 'r', encoding='utf-8') as fo:
        '''
            //Because loads need to pass in a memory object, so
            //You need to save it temporarily in memory, and then use loads to do it.
            //Data reading operation
        '''
        data = fo.read()
        d2= json.loads(data)  
    print(d2, type(d2))

The printing effect is as follows:

Writing of JSON Data

The json module also provides two writing methods, dump and dumps, as follows:

def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
        allow_nan=True, cls=None, indent=None, separators=None,
        default=None, sort_keys=False, **kw):
    """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
    ``.write()``-supporting file-like object)."""
def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
        allow_nan=True, cls=None, indent=None, separators=None,
        default=None, sort_keys=False, **kw):
    """Serialize ``obj`` to a JSON formatted ``str``."""

In fact, the two are much the same as reading, they are different objects needed.

So here's a data:

    data = {
        'id': '2016982',
        'title': 'Heading 1',
        'publish_data': '2019/4/15',
        'publish_place': 'Shenzhen',
        'software': 'lg',
    }

Writing files with dump and dumps respectively

    with open('datas.json', 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4, separators=(', ', ': '))

    with open('datas.json', 'w', encoding='utf-8') as f:
        # Format a dictionary into a string object
        f.write(json.dumps(data2, ensure_ascii=False, indent=4, separators=(', ', ': ')))

After writing, you will get a file named datas.json, which reads as follows:

{
        'id': '2016982',
        'title': 'Heading 1',
        'publish_data': '2019/4/15',
        'publish_place': 'Shenzhen',
        'software': 'lg',
    }

The role of each parameter in the method

parameter Effect
skipkeys If skipkeys are true (default is False), dictionary keys that are not basic objects (including str, int, float, bool, None) will be skipped; otherwise, a TypeError will be triggered.
ensure_ascii If ensure_ascii is true (that is, the default value), the output guarantees that all input non-ASCII characters are escaped. If ensure_ascii is false, these characters will be output as they are.
check_circular If check_circular is a false value (default is True), circular reference checking of container type is skipped and circular reference causes an Overflow Error (or worse).
allow_nan If allow_nan is false (default is True), a ValueError will be triggered when serializing float type values (nan, inf, and - inf) outside the strict JSON specification. If allow_nan is true, use their JavaScript equivalents (NaN, Infinity, and - Infinity).
indent If indent is a non-negative integer or string, JSON array elements and object members are beautified to output the indentation level specified by that value. If the indentation level is zero, negative or "", only newline characters will be added. None (default) chooses the most compact expression. Using a positive integer will indent each layer with the same number of spaces. If * indent * is a string (such as " t"), that string will be used to indent each layer.
separators When specified, separators should be an item_separator, key_separator tuple. When indent is None, the default value is (',',':'), otherwise (',':'). To get the most compact JSON expression, you should specify it as (',',':') to eliminate the blank characters.
sort_keys If sort_keys is true (default is False), the dictionary output is sorted in the order of keys.
object_hook Object_hook is an optional function that is invoked for each decoded object literal quantity (that is, a dict). The return value of object_hook replaces the original dict. This feature can be used to implement custom decoders (such as JSON-RPC type hints).

Note: The above parameters are from official documents. https://docs.python.org/3/library/json.html

Posted by JH5 on Wed, 24 Apr 2019 12:45:35 -0700