Polyglot CheatSheet - JSON
Last Updated: 2023-03-02
JavaScript
JSON is exposed by V8
JSON.stringify(object)
: convert object to stringJSON.parse(string)
: convert string to object
To pretty print(indent by 4 spaces):
JSON.stringify(data, null, ' ');
or
JSON.stringify(data, null, 4);
Python
Pretty print JSON by python from command line:
$ cat data.json | python -m json.tool
From Python code:
# Import
import json
# Load From File
data = json.load(open("./data.json"))
# Write To File
with open('./data.json', 'w') as fp:
json.dump(data, fp, indent=2)
# Dump As String
print(json.dumps(foo, indent=4))