5

This is json

{"name":"david","age":14,"gender":"male"}

This is python class

class Person:
    def __init__(self):
        self.name = None
        self.age = None
        self.gener = None
        self.language = None

this is Code

#deserialize func~~~~~
print person.name #prints david
print person.age #prints 14
print person.gender #prints male
print person.language #prints "None"

Can I deserialize Json to class in Python(like C# Newtonsoft)

Thank you.

5
  • It's been tackled: stackoverflow.com/questions/15476983/… Commented Mar 4, 2017 at 13:44
  • 2
    Possible duplicate of Deserialize a json string to an object in python Commented Mar 4, 2017 at 13:44
  • I want to matching all json item, like person. language is None from this json {"name":"david","age":14,"gender":"male"} Commented Mar 4, 2017 at 13:48
  • @ambitiousdh If you don't want the None values do a dictionary comprehension and remove all attributes that are None i.e {k: v for k, v in self.__dict__ if v} Commented Mar 4, 2017 at 13:58
  • No, I want to do mapping even if it is not in json Item. Commented Mar 4, 2017 at 14:04

2 Answers 2

6

You can use it with the json.loads() method. You would also need to ensure your JSON was a string and not just declared inline.

Here's an example program:

import json

js = '{"name":"david","age":14,"gender":"male"}'

class Person:
    def __init__(self, json_def):
        self.__dict__ = json.loads(json_def)

person = Person(js)

print person.name
print person.age
print person.gender

Just a note, though. When you attempt to use print person.language you will have an error, since it doesn't exist on the class.

EDIT

If there is a direct mapping desired, it would require explicit mapping of each possible object.

The following example will give each property a value if it exists in the JSON object and also solves the desire to use any missing properties:

import json

js = '{"name":"david","age":14,"gender":"male"}'

class Person(object):
    def __init__(self, json_def):
        s = json.loads(json_def)
        self.name = None if 'name' not in s else s['name']
        self.age = None if 'age' not in s else s['age']
        self.gender = None if 'gender' not in s else s['gender']
        self.language = None if 'language' not in s else s['language']

person = Person(js)

print person.name
print person.age
print person.gender
print person.language
Sign up to request clarification or add additional context in comments.

Comments

5

You can use the **kwargs like approach when we loads a json we will get a dict then we can just initialize our object with **json_def and get our object. for more complex json deserialization you will have to check the dict and take only the values that you need.

example:

class Person:
    def __init__(self, name=None, age=None, gender=None, language=None):
        self.name = name
        self.age = age
        self.gender = gender
        self.language = language


js = '{"name": "david", "age": 14, "gender": "male"}'
person = Person(**json.loads(js))
print(person.name)
js = '{"name": "david", "age": 14, "gender": "male", "language": "English"}'
person = Person(**json.loads(js))
print(person.language)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.