JSON processing in Python: Difference between revisions
Jump to navigation
Jump to search
Line 7: | Line 7: | ||
=Overview= | =Overview= | ||
=Serialization= | =Serialization= | ||
Serialization into a string: | |||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
with open("data_file.json", "w") as f: | with open("data_file.json", "w") as f: | ||
json.dump(data, f) | json.dump(data, f) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Serialization into a file | |||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
with open("data_file.json", "w") as f: | |||
with open(" | |||
json.dump(data, f) | json.dump(data, f) | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 01:16, 9 March 2022
External
Internal
Overview
Serialization
Serialization into a string:
with open("data_file.json", "w") as f:
json.dump(data, f)
Serialization into a file
with open("data_file.json", "w") as f:
json.dump(data, f)
Deserialization
import json
data = json.loads('{"color":"blue", "size":10, "details":{"shape":{"texture":"bumpy", "orientation":"vertical"}}}')
print(data.get("color")) # displays blue
print(data["color"]) # displays blue
print(data.get("size")) # displays 10
print(data["details"]["shape"]["texture"]) # displays bumpy
Suggestions on how to safely recursively navigate a complex data structure: