Back
Close

Computing with Data

elgeish
34.9K views
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
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants