To serialize a JSON string into a Python object:
import json
json_str = '[{"first": "bob", "last": "smith"}]'
json_obj = json.loads(json_str)
The json_obj variable will contain an Python list that can be iterated over:
for info in json_obj:
print('First: {0}; Last: {1}'.format(info["first"], info["last"]))
The above code will print:
First: bob; Last: smith
Here's how to deserialize a Python object into a JSON string:
import json
json_obj = [{"first": "bob", "last": "smith"}]
json_str = json.dumps(json_obj)
The json_str variable will contain a string:
[{"first": "bob", "last": "smith"}]
Leave a comment