Sometimes, we want to get JSON to load into an OrderedDict with Python.
In this article, we’ll look at how to get JSON to load into an OrderedDict with Python.
How to get JSON to load into an OrderedDict with Python?
To get JSON to load into an OrderedDict with Python, we can use the json.loads
method.
For instance, we write
import json
data = json.loads('{"foo":1, "bar":2, "fiddle":{"bar":2, "foo":1}}')
print(json.dumps(data, indent=4))
to call json.loads
with the JSON string to load the string into an ordered dict.
Then we call json.dumps
with data
and indent
set to 4 to pretty print the JSON with 4 spaces for indentation at each level.
Conclusion
To get JSON to load into an OrderedDict with Python, we can use the json.loads
method.