Tuesday 21 August 2018

Python - III

After seeing file handling in this post, and string handling in this post, we will take a look at how JSON is handled in Python in this post. For all the work in this post, like before, we will be using CLI of Python 3.7.0(the latest version of Python as of the time this blog is being written).

There are four methods that can be used for encoding and decoding JSON in Python. They are dump, dumps, load and loads. The method dump serializes an object as a JSON formatted stream and stores it as a file. Encoding into JSON will use below conversion table:












For the first example, let us consider a string called data that is encoded into JSON using dump. dump takes a few parameters that are shown in examples below.

>>> import json
>>> data = {"connection": {'Latitude': 41.556895, 'Longitude': -72.665225}, "loc": [-72.665225, 41.556895], "loc_tup": (-72.665225, 41.556895), "state": "CT", "pop": 42846, "enroute": True, "travel": None}
>>> json.dump(data, open('json_file.json','w'))  # we can create a file and write to it

Now, let us examine the file:









Note the encoding conversion of the elements from Python to JSON as per conversion table above. To read this file in prompt:

F:\PythonPrograms>type json_file.json
{"connection": {"Latitude": 41.556895, "Longitude": -72.665225}, "loc": [-72.665225, 41.556895], "loc_tup": [-72.665225, 41.556895], "state": "CT", "pop": 42846, "enroute": true, "travel": null}

To pretty print it, use below command:

F:\PythonPrograms>type json_file.json | python -m json.tool
{
    "connection": {
        "Latitude": 41.556895,
        "Longitude": -72.665225
    },
    "loc": [
        -72.665225,
        41.556895
    ],
    "loc_tup": [
        -72.665225,
        41.556895
    ],
    "state": "CT",
    "pop": 42846,
    "enroute": true,
    "travel": null
}

F:\PythonPrograms>

Instead of using dump to write the JSON to a file, we can also use StringIO() that reads and writes a string buffer:

>>> from io import StringIO
>>> io = StringIO()
>>> json.dump(data, io)
>>> io.getvalue()
'{"connection": {"Latitude": 41.556895, "Longitude": -72.665225}, "loc": [-72.665225, 41.556895], "loc_tup": [-72.665225, 41.556895], "state": "CT", "pop": 42846, "enroute": true, "travel": null}'

>>>

In the next example, we will add a few parameters. For this example, we will use another string dump_1:

data_1 = {"connection": {'Longitude': -72.665225, 'Latitude': 41.556895}, "loc": [-72.665225, 41.556895], "loc_tup": (-72.665225, 41.556895), "state": "CT", "pop": 42846, "enroute": True, "travel": None, "special": "ÃΣ"}

>>>
>>> json.dump(data_1, open('json_file_1.json','w'), ensure_ascii=True, indent=None, separators=(',', ':'), sort_keys=True)

>>>

Let us now inspect the created file using type:

F:\PythonPrograms>
F:\PythonPrograms>type json_file_1.json
{"connection":{"Latitude":41.556895,"Longitude":-72.665225},"enroute":true,"loc":[-72.665225,41.556895],"loc_tup":[-72.665225,41.556895],"pop":42846,"special":"\u00c3\u03a3","state":"CT","travel":null}

F:\PythonPrograms>

ensure_ascii=True ensures that the output is guaranteed to have all incoming non-ASCII characters escaped. See the encoding on ÃΣ. json.dump(data_1, open('json_file_1.json','w'), ensure_ascii=True, indent=None, separators=(',', ':'), sort_keys=True) removes any whitespace. Compare current type output with previous type output to see the difference. sort_keys=True sorts the keys of any dictionaries in the input. In our case, see the reordering of "connection" dictionary.

dumps is similar to dump expect that the input object is serialized to a JSON string using the same conversion table that dump uses. It also uses the same parameters as dump but here we do not specify any file name. An example is shown below:

data_1 = {"connection": {'Longitude': -72.665225, 'Latitude': 41.556895}, "loc": [-72.665225, 41.556895], "loc_tup": (-72.665225, 41.556895), "state": "CT", "pop": 42846, "enroute": True, "travel": None, "special": "ÃΣ"}

>>> json.dumps(data_1, ensure_ascii=True, indent=None, separators=(',', ':'), sort_keys=True)
'{"connection":{"Latitude":41.556895,"Longitude":-72.665225},"enroute":true,"loc":[-72.665225,41.556895],"loc_tup":[-72.665225,41.556895],"pop":42846,"special":"\\u00c3\\u03a3","state":"CT","travel":null}'

>>>

To pretty print the output, we can set indent to 4 as shown below:

>>> print(json.dumps(data_1, ensure_ascii=True, indent=4, separators=(',', ':'), sort_keys=True))
{
    "connection":{
        "Latitude":41.556895,
        "Longitude":-72.665225
    },
    "enroute":true,
    "loc":[
        -72.665225,
        41.556895
    ],
    "loc_tup":[
        -72.665225,
        41.556895
    ],
    "pop":42846,
    "special":"\u00c3\u03a3",
    "state":"CT",
    "travel":null
}
>>>

load deserializes a JSON file to a Python object according to below conversion table:














We will decode the encoded JSON file, json_file_1.json, as shown below:

>>>
>>> json.load(open('json_file_1.json'))
{'connection': {'Latitude': 41.556895, 'Longitude': -72.665225}, 'enroute': True, 'loc': [-72.665225, 41.556895], 'loc_tup': [-72.665225, 41.556895], 'pop': 42846, 'special': 'ÃΣ', 'state': 'CT', 'travel': None}
>>>

You can see the converted values.

loads is similar to load but takes encoded JSON string as shown below:

>>> data_1 = {"connection": {'Longitude': -72.665225, 'Latitude': 41.556895}, "loc": [-72.665225, 41.556895], "loc_tup": (-72.665225, 41.556895), "state": "CT", "pop": 42846, "enroute": True, "travel": None, "special": "ÃΣ"}
>>>
>>> encoded_JSON =  json.dumps(data_1, ensure_ascii=True, indent=None, separators=(',', ':'), sort_keys=True)
>>>
>>> json.loads(encoded_JSON)
{'connection': {'Latitude': 41.556895, 'Longitude': -72.665225}, 'enroute': True, 'loc': [-72.665225, 41.556895], 'loc_tup': [-72.665225, 41.556895], 'pop': 42846, 'special': 'ÃΣ', 'state': 'CT', 'travel': None}
>>>

This concludes the discussion of JSON in Python