Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python convert json string to class

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

person_string = '{"name": "Bob", "age": 25}'

person_dict = json.loads(person_string)
person_object = Person(**person_dict)

print(person_object)
Comment

python convert json string to module object class

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 = '{"rollNumber": 1, "name": "Emma"}'

# 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)
Comment

python convert json string to module object class

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)
Comment

PREVIOUS NEXT
Code Example
Python :: taking array input in python 
Python :: how to get value from set in python 
Python :: all select first value in column list pandas 
Python :: tkinter dialog box 
Python :: python random array shuffle 
Python :: pandas .nlargest 
Python :: how to check all the elements in a list are even or not 
Python :: how to check a string is palindrome or not in python 
Python :: read and write to file python 
Python :: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead 
Python :: xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 0 
Python :: access env variable in flask 
Python :: for loop from n to 1 in python 
Python :: starting variable name with underscore python 
Python :: qt designer messagebox python 
Python :: number of spaes pythopn 
Python :: how to empty a dictionary in python 
Python :: How do I iterate over a subfolder in Python 
Python :: play video in python console 
Python :: convert a string into a list in Python 
Python :: Matplotlib inside Jupyter | Jupyter generate graphs. 
Python :: python string cut first n characters 
Python :: check auth user django 
Python :: pygame text wrapping 
Python :: how to iterate over a list in python 
Python :: select default option django form 
Python :: dictionary get all keys 
Python :: sklearn regression 
Python :: inser elemts into a set in python 
Python :: django sample 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =