Q:

python object to json


  import json

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New 
  York"}'

# parse x:
y = json.loads(x)
1
# a Python object (dict):
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

# convert into JSON:
y = json.dumps(x)
2
jsonStr = json.dumps(myobject.__dict__)
0
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)
0
import json

x = {
  "name": 
  "John",
  "age": 30,
  "city": "New York"
}

y = json.dumps(x)
0

New to Communities?

Join the community