2 Easy Ways to Serialize and De-serialize JSON to objects in Python

Filed Under: Python Modules
Serialize & De Serialize JSON Data

Hello, readers! In this article, we will be focusing on 2 Easy Ways to Serialize and De-serialize JSON to objects in Python.

So, let us begin! 馃檪

What is JSON?

Just to give a quick overview, JSON data is the best form of the human-readable code file. It stores the data values in a key-value pair. This, in turn, makes it easy for us to refer to the data easily by calling up the key values.

In the same process, we come across API calls and other sources through which we can obtain bulks of data. The same data needs to be processed in a human-readable form. This is when JSON format comes into the picture. On the contrary, having JSON data with us, we often feel the need to convert it into the native data type object for us to use in the application for processing.

Considering the above scenarios, we present to you 2 Easy ways to Serialize and De-Serialize JSON data in Python.

Let us now have a look at these techniques and have a brief understanding about the concepts in the upcoming section.


How to Serialize JSON data in Python?

Serialization is the process of conversion of the native/primary data type of the data into a JSON format. With this, the data usually in the form of dict, list can be converted into a JSON format.

For the same, Python offers us with the below methods to perform serialization–

  1. The json.dump() method
  2. Python json.dumps() method

1. Python json.dump() method

The json.dump() method accepts the native data of the primary data type as input. Further, it processes the data into a JSON format and then stores it into a JSON file.

Syntax:

json.dump(data, file-pointer)
  • data: The data to be converted to JSON format.
  • file-pointer: The object pointing to the JSON file within which data needs to be stored. If the file does not exist, if creates a new one at the pointed location.

Example:

In this example, we have converted the data of type dictionary to JSON data and have stored it in data.json file using json.dump() function.

import json

info = {
	"data": {
		"name": "Johan James",
		"subject": "Maths",
		"City": "Pune"
	}
}

with open( "data.json" , "w" ) as x:
	json.dump( info , x )

Output:

Serialize JSON To Objects
Serialize JSON To Objects

2. Python json.dumps() function

The json.dumps() method accepts dictionary value as input and converts it into JSON data. Rather than storing it in a file, it stores the data into a JSON string format.

import json

info = {
	"data": {
		"name": "Johan James",
		"subject": "Maths",
		"City": "Pune"
	}
}

data = json.dumps( info )
print( data )

Output:

{"data": {"name": "Johan James", "subject": "Maths", "City": "Pune"}}

How to De-serialize JSON to a native Python object?

De-serialization is the process of decoding the JSON format data into the native data type object. That is, it converts the JSON data into the native data type usually a dictionary.

For the same, Python offers us with the below functions to de-serialize JSON objects–

  1. json.load() method
  2. json.loads() method

1. Python json.load() method

With json.load() method, we can convert the JSON string data into a native dictionary object in Python.

Syntax:

json.load(JSON string data)

Example:

In the below example, we have processed and converted the JSON data into dictionary object using json.load() function.

import json

info = '{"name": "Johan James","subject": "Maths"}'
print("Datatype of the unserialized JSON data : "
      + str(type(info)))

res = json.loads(info)

print(res) 
print("Datatype of the serialized JSON data : " + str(type(res)))

Output:

Datatype of the unserialized JSON data : <class 'str'>
{'name': 'Johan James', 'subject': 'Maths'}
Datatype of the serialized JSON data : <class 'dict'>

2. Python json.loads() method

With json.loads() method, we can convert a JSON file into a native dictionary data. For the same, we need to pass the file pointer to the json.load() function, it processes the data at the location and results into dictionary data object.

Example:

In this example, we have converted the data from data.json file into native dictionary object using json.loads() method.

import json

info = open('data.json',)

res = json.load(info)

print(res)
print("Datatype post deserialization : " + str(type(res)))

Output:

{'data': {'name': 'Johan James', 'subject': 'Maths', 'City': 'Pune'}}
Datatype post deserialization : <class 'dict'>

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content