Json decode object python

Python Convert JSON data Into a Custom Python Object

In this article, we will learn how to convert JSON data into a custom Python object. i.e., Parse and convert JSON into Python Class. For example, you receive employee JSON data from the API or you are reading JSON from a file and wanted to convert it into a custom Employee type.

You know how to encode Python object into JSON. When you load JSON data from file or String using the json.load() and json.loads() method, it returns a dict .

If we load JSON data directly into our custom type we can manipulate and use it more effortlessly. There are various ways to achieve this. You can pick the way you find it more useful for your problem. Let’s see how to deserialize a JSON string to a custom Python object.

Further Reading:

Explained how to Convert JSON into custom Python Object

Using namedtuple and object_hook to Convert JSON data Into a Custom Python Object

We can use the object_hook parameter of the json.loads() and json.load() method. The object_hook is an optional function that will be called with the result of any object literal decoded (a dict). So when we execute json.loads() , The return value of object_hook will be used instead of the dict . Using this feature, we can implement custom decoders.

Читайте также:  Christmas Contest Winners

To convert JSON into a custom Python type we need to follow the following:

As we know json.load() and json.loads() method convert JSON into a dict object so we need to create a custom function where we can convert dict into a custom Python type. and pass this newly created function to an object_hook parameter of a json.loads method. so we can get custom type at the time of decoding JSON.

The namedtuple is class, under the collections module. Like the dictionary type objects, it contains keys and that are mapped to some values. In this case, we can access the elements using keys and indexes.

Let’ see the simple example first then we can move to the practical example. In this example, we are converting Student JSON data into a custom Student Class type.

import json from collections import namedtuple from json import JSONEncoder def customStudentDecoder(studentDict): return namedtuple('X', studentDict.keys())(*studentDict.values()) #Assume you received this JSON response studentJsonData = '' # Parse JSON into an object with attributes corresponding to dict keys. student = json.loads(studentJsonData, object_hook=customStudentDecoder) print("After Converting JSON Data into Custom Python Object") print(student.rollNumber, student.name)
After Converting JSON Data into Custom Python Object 1 Emma

As you can see we converted JSON data which was present in the JSON String format into a custom Python object Student. Now, we can access its members using a dot(.) operator.

Now, let’s see the realtime scenario where work with complex Python Objects. And we need to convert custom Python object into JSON. Also, we want to construct a custom Python object from JSON.

In this example, we are using two classes Student and Marks . A Marks class is a member of the Student class.

  • First, we encode the Student class into JSON Data.
  • Then, we use the same JSON data to decode it into a Student class

Let’ see the example now.

import json from collections import namedtuple from json import JSONEncoder class Student: def __init__(self, rollNumber, name, marks): self.rollNumber, self.name, self.marks = rollNumber, name, marks class Marks: def __init__(self, english, geometry): self.english, self.geometry = english, geometry class StudentEncoder(JSONEncoder): def default(self, o): return o.__dict__ def customStudentDecoder(studentDict): return namedtuple('X', studentDict.keys())(*studentDict.values()) marks = Marks(82, 74) student = Student(1, "Emma", marks) # dumps() produces JSON in native str format. if you want to writ it in file use dump() studentJson = json.dumps(student, indent=4, cls=StudentEncoder) print("Student JSON") print(studentJson) # Parse JSON into an object with attributes corresponding to dict keys. studObj = json.loads(studentJson, object_hook=customStudentDecoder) print("After Converting JSON Data into Custom Python Object") print(studObj.rollNumber, studObj.name, studObj.marks.english, studObj.marks.geometry)
Student JSON < "rollNumber": 1, "name": "Emma", "marks": < "english": 82, "geometry": 74 >> After Converting JSON Data into Custom Python Object 1 Emma 82 74

Using types.SimpleNamespace and object_hook to convert JSON data Into a Custom Python Object

We can use types.SimpleNamespace as the container for JSON objects. It offers the following advantages over a namedtuple solution: –

  • Its execution time is less because it does not create a class for each object.
  • It is precise and simplistic

In this example, we will use a types.SimpleNamespace and object_hook to convert JSON data into custom Python Object.

from __future__ import print_function import json from json import JSONEncoder try: from types import SimpleNamespace as Namespace except ImportError: # Python 2.x fallback from argparse import Namespace class Student: def __init__(self, rollNumber, name, marks): self.rollNumber, self.name, self.marks = rollNumber, name, marks class Marks: def __init__(self, english, geometry): self.english, self.geometry = english, geometry class StudentEncoder(JSONEncoder): def default(self, o): return o.__dict__ marks = Marks(82, 74) student = Student(1, "Emma", marks) # dumps() produces JSON in native str format. if you want to writ it in file use dump() studentJsonData = json.dumps(student, indent=4, cls=StudentEncoder) print("Student JSON") print(studentJsonData) # Parse JSON into an custom Student object. studObj = json.loads(studentJsonData, object_hook=lambda d: Namespace(**d)) print("After Converting JSON Data into Custom Python Object using SimpleNamespace") print(studObj.rollNumber, studObj.name, studObj.marks.english, studObj.marks.geometry)
Student JSON < "rollNumber": 1, "name": "Emma", "marks": < "english": 82, "geometry": 74 >> After Converting JSON Data into Custom Python Object using SimpleNamespace 1 Emma 82 74

Using object decoding of a JSONDecoder class to convert JSON data Into a Custom Python Object

We can use the json.JSONDecoder class of json module to specialize JSON object decoding, here we can decode a JSON object into a custom Python type.

We need to create a new function in a class that will be responsible for checking object type in JSON string, after getting the correct type in the JSON data we can construct our Object.

import json class Student(object): def __init__(self, rollNumber, name, marks): self.rollNumber = rollNumber self.name = name self.marks = marks def studentDecoder(obj): if '__type__' in obj and obj['__type__'] == 'Student': return Student(obj['rollNumber'], obj['name'], obj['marks']) return obj studentObj = json.loads('', object_hook=studentDecoder) print("Type of decoded object from JSON Data") print(type(studentObj)) print("Student Details") print(studentObj.rollNumber, studentObj.name, studentObj.marks)
Type of decoded object from JSON Data Student Details 1 Ault kelly 78

Use jsonpickle module to convert JSON data into a custom Python Object

jsonpickle is a Python library designed to work with complex Python Objects. You can use jsonpickle for serialization and deserialization complex Python and JSON Data. You can refer to Jsonpickle Documentation for more detail.

The built-in JSON module of Python can only handle Python primitives. For any custom Python object, we need to write our own JSONEncoder and Decoder.

Using jsonpickle we will do the following: –

  • First, we will encode Student Object into JSON using jsonpickle
  • Then we will decode Student JSON into Student Object

Now, let’s see the jsonpickle example to convert JSON data Into a Custom Python Object.

import json import jsonpickle from json import JSONEncoder class Student(object): def __init__(self, rollNumber, name, marks): self.rollNumber = rollNumber self.name = name self.marks = marks class Marks(object): def __init__(self, english, geometry): self.english = english self.geometry = geometry marks = Marks(82, 74) student = Student(1, "Emma", marks) print("Encode Object into JSON formatted Data using jsonpickle") studentJSON = jsonpickle.encode(student) print(studentJSON) print("Decode and Convert JSON into Object using jsonpickle") studentObject = jsonpickle.decode(studentJSON) print("Object type is: ", type(studentObject)) print("Student Details") print(studentObject.rollNumber, studentObject.name, studentObject.marks.english, studentObject.marks.geometry)
Encode Object into JSON formatted Data using jsonpickle , "name": "Emma", "py/object": "__main__.Student", "rollNumber": 1> Decode JSON formatted Data using jsonpickle 1 Emma 82 74

Create a new Object, and pass the result dictionary as a map to convert JSON data into a custom Python Object

As we know json.loads() and json.load() method returns a dict object. we can construct a new custom object by passing the dict object as a parameter to the Student Object constructor. i.e., we can map the dict object to a custom object.

import json from json import JSONEncoder class Student(object): def __init__(self, rollNumber, name, *args, **kwargs): self.rollNumber = rollNumber self.name = name class StudentEncoder(JSONEncoder): def default(self, o): return o.__dict__ student = Student(1, "Emma") # encode Object it studentJson = json.dumps(student, cls=StudentEncoder, indent=4) #Deconde JSON resultDict = json.loads(studentJson) print("Converting JSON into Python Object") studentObj = Student(**resultDict) print("Object type is: ", type(studentObj)) print("Student Details") print(studentObj.rollNumber, studentObj.name)
Converting JSON into Python Object Object type is: Student Details 1 Emma

So What Do You Think?

I want to hear from you. What do you think of this article? Or maybe I missed one of the ways to Convert JSON data Into a Custom Python Object. Either way, let me know by leaving a comment below.

Also, try to solve the Python JSON Exercise to have a better understanding of Working with JSON Data in Python.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Оцените статью