Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Debugging Next: Pickling

JSON Serialization

Note that JSON is very similar to legitimate Python code, making Python particularly easy for interacting with Python code:

import json
# creating a Python object (map) holding a person's address
personRecord = {
"firstName": "Jane",
"lastName": "Doe",
"yearOfBirth": 1975,
"address": {
"streetAddress": "100 Main Street",
"city": "Los Angeles",
"state": "CA",
"postalCode": "90021"
},
"phoneNumbers": [
{ "type": "home", "number": "(444) 555-1234" },
{ "type": "office", "number": "(444) 555-1235" }
]
}
# convert the Python object into a JSON string
personRecordJSON = json.dumps(personRecord)
personRecordReconstructed = json.loads(personRecordJSON)
# print the firstName field of the reconstructed Python object
print(personRecordReconstructed['firstName'])
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content