Python Serialize and Deserialize JSON to Objects

What is JSON?

JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces { }.

In Python, How do you serialize JSON data?

Serialization is the process of converting the raw data’s data type to JSON representation. That is to say, the raw data, which is typically a dictionary, will now match the Javascript Object Notation format.

Python provides us with the following functions to simply format our data to JSON:

  • json.dump() function
  • json.dumps() function

1)json.dump() function:

The native data of the primary data type is accepted as input by the json.dump() method. It then converts the data to JSON format and stores it in a JSON file.

Syntax:

json.dump(data, fileobject)

data: It is the actual data that needs to be converted to JSON format is referred to as data.

fileobject: This is the object that will point to the JSON file containing the transformed data. If the file does not already exist, a new file is created at the location specified by the object.

Example

Approach:

  • Import json module using the import keyword.
  • Give some random data in JSON format and store it in a variable.
  • Open some random file in write-mode using the open() function by passing the filename, mode as arguments to it.
  • Dump the given data into the above-opened file using the dump() function. It then converts the data to JSON format.
  • The Exit of the Program.

Below is the implementation:

# Import json module using the import keyword
import json
# Give some random data in Json format and store it in a variable
gvn_data= {
    "demo_data": {
        "Article By": "Btechgeeks",
         "topic": "JSON",
         "language": "Python"
    }
}
# Open some random file in write-mode using the open() function by passing
# the filename, mode as arguments to it.
with open( "demo.json" , "w" ) as d:
    # Dump the given data into the above opened file using the dump() function
    json.dump( gvn_data, d )

Output:

Raw data converted to JSON format(Output)
Raw data converted to JSON format(Output)

2)json.dumps() function

In contrast to the dump() function, the json.dumps() function converts raw data to JSON format but stores it as a string rather than pointing to a file object.

Syntax:

json.dumps(data)

Example1

Approach:

  • Import json module using the import keyword.
  • Give some random data in JSON format and store it in a variable.
  • Pass the given data as an argument to the dumps() function to convert raw data to JSON format (It stores it as a string rather than pointing to a file object).
  • Store it in a variable.
  • Print the above result

Below is the implementation:

# Import json module using the import keyword
import json
# Give some random data in Json format and store it in a variable
gvn_data= {
    "demo_data": {
        "Article By": "Btechgeeks",
         "topic": "JSON",
         "language": "Python"
    }
}
# Pass the given data as an argument to the dumps() function to convert raw data to
# JSON format.(It stores it as a string rather than pointing to a file object)
# Store it in a variable
rslt_data = json.dumps(gvn_data)
# print the above result
print(rslt_data)

Output:

{"demo_data": {"Article By": "Btechgeeks", "topic": "JSON", "language": "Python"}}

De-serialization of JSON data to Native Python Object

That is, we can simply convert JSON data into the default/native data type, which is usually a dictionary, using deserialization.

Python provides the following functions to implement the concept of de-serialization:

  • json.load() function
  • json.loads() function

json.load() Function:

We can convert JSON string data into a native dictionary object in Python using the json.load() function.

Syntax:

json.load(JSON _strdata)

Approach:

  •  Import json module using the import keyword.
  • Open the JSON file using the open() function by passing the filename/filepath
    as an argument to it and store it in a variable.
  • Pass the above JSON file data as an argument to the load() function to deserialize it.
    Store it in another variable.
  • Print the above result.
  • Print the datatype of the above result using the type() function.

Below is the implementation:

# Import json module using the import keyword
import json
# Open the JSON file using the open() function by passing the filename/filepath 
# as an argument to it and store it in a variable
file_data = open('demo.json')
# Pass the above JSON file data as an argument to the load() function to deserialize it
# Store it in another variable
rslt = json.load(file_data)
# Print the above result
print(rslt)
# Print the datatype of the above result using the type() function.
print("After de-serialization, the datatype of the above result is:\n", type(rslt))

Output:

{'demo_data': {'Article By': 'Btechgeeks', 'topic': 'JSON', 'language': 'Python'}}
After de-serialization, the datatype of the above result is:
<class 'dict'>

Here, we first used the open() function to load the JSON file. Following that, we give the object corresponding to the JSON file to the load() function, where it is deserialized into dictionary form.