Python JSON Dump Vs. Load: What’s the Difference

When applications are heterogeneous, JSON acts as a data exchange format since many applications support this format. The JSON is in Key/Value format. Python supports JSON files. Here are examples of how to use the Dump, Load, and Loads.

Here you’ll know about:

  1. JSON File format
  2. How to create a JSON file
  3. How to use Dump
  4. How to use Load
  5. How to use Loads

JSON is also called Javascript Object Notion. Python reads JSON files, and you can write JSON data to a file. Both these are possible in Python.

JSON File format

Here is a simple JSON file shows of employee data.

data={
"employee": {
        "name": "Vas",
        "doj": "20-JAN-1999"
            }
    }

How to create a JSON file

How to use Dump

The dump writes the JSON data to a new file. The Dump writes data to the output file. In this case, the output.json is the output file. This’s how you can use the Dump.

import json
f=open("outfile.json", "w")
json.dump(data, f)
f.close()

f=open("outfile.json", "r")
print(f.read())

Here’s the output you’ll get:

{"employee": {"name": "Vas", "doj": "20-JAN-1999"}}


** Process exited - Return Code: 0 **
Press Enter to exit terminal

How to use Load

Before you know more about the Load, you see the scenario of the reading file. Here, exactly, the Load does that. The Loaded file assigns to an object, which then use to know individual values.

data={
"employee": {
        "name": "Vas",
        "doj": "20-JAN-1999"
            }
    }


import json

f=open("outfile.json", "w")
json.dump(data, f)
f.close()

f=open("outfile.json")
a=json.load(f)
print(a["employee"]["name"])

Here’s the output

Vas

** Process exited - Return Code: 0 **
Press Enter to exit terminal

How to use Loads

Even though I have used the same JSON file, it has thrown an error.

data={
"employee": {
        "name": "Vas",
        "doj": "20-JAN-1999"
            }
    }


import json

f=open("outfile.json", "w")
json.dump(data, f)
f.close()

f=open("outfile.json")
a=json.loads(f)
print(a["employee"]["name"])

Here’s the output with the error.

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    a=json.loads(f)
  File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper


** Process exited - Return Code: 1 **
Press Enter to exit terminal

Finally, The difference is minimal, and you can choose which way works best for you. The main reason to use loads, rather than load, is that you can work with strings input from the user /or the one stored within your application.

The JSON.load requires that the entire file is in standard JSON format, so if your file contains other information, you should load the JSON string first and parse it with loads rather than using load directly.

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Start Discussion

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.