JSON processing in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
data = ...
data = ...
with open("data_file.json", "w") as f:
with open("data_file.json", "w") as f:
    json.dump(data, f)
</syntaxhighlight>
At stdout
<syntaxhighlight lang='python'>
import json
data = ...
with open("/dev/stdout", "w") as f:
     json.dump(data, f)
     json.dump(data, f)
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:08, 9 March 2022

External

Internal

Overview

Serialization

import json

data = ...
with open("data_file.json", "w") as f:
    json.dump(data, f)

At stdout

import json

data = ...
with open("/dev/stdout", "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

Safely Navigate a Complex Data Structure

Suggestions on how to safely recursively navigate a complex data structure:

Safely Navigate a Complex Data Structure