Cheatsheet: Data Structures
    Array / List
    Map / Dict
    Set
    Tuple
    Struct / Data Class
    Stacks
    Other Data Structures
    Collection Conversions
Cheatsheet: Control Flow
Cheatsheet: Class

Polyglot CheatSheet - JSON

Updated: 2022-04-25

JavaScript

JSON is exposed by V8

  • JSON.stringify(object): convert object to string
  • JSON.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

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))