string class to json object python code example
Example 1: python json string to object
import json
x = '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)
print(y["age"])
Example 2: convert class object to json python
import json
class Laptop:
name = 'My Laptop'
processor = 'Intel Core'
#create object
laptop1 = Laptop()
laptop1.name = 'Dell Alienware'
laptop1.processor = 'Intel Core i7'
#convert to JSON string
jsonStr = json.dumps(laptop1.__dict__)
#print json string
print(jsonStr)