logo

JSON Cheatsheet

JSON is actually a subset of YAML.

By Language

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

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

Go

Use struct tags to name the fiels.

type Person struct {
     Name string `json:"name"` // Use "name" instead of "Name" for the field name.
     Age int `json:"age"`
}