James's Ramblings

json

Created: February 03, 2019
  • JSON files are a collection of comma-separated key-value pairs. "key": VALUE,
  • JSON starts and ends with curly brackets. { JSON_HERE }
  • Whitespace does not matter.
  • Keys are always strings.
  • Keys should be unique. Some JSON implementations use the last key if multiple are given, others throw an error.
  • Strings must always be enclosed in double quotes.
  • All unicode is valid. Characters can be escaped with a backslash. "\"" = ".

Data Types

Data Type Notated By Example
Strings “STRING” "string": "value"
Integer INT "integer": 1
Floating-point numbers x.yy or xEy or x.yE{+|-}z "key": 1.00
Arrays [] [1,2,3,"GO"]
Objects {} "object": {"i": 1, "y": 2}
Boolean true or false "boolean": false
Null null "null": null
  • E = E (exponent) notation.
  • Arrays contain comma-separated lists of values. Multiple types can be used in a single array.
  • Objects contain comma-separated collections of key-value pairs.
  • Objects can exist within arrays, and arrays can exist within objects.
  • When objects exist within arrays, no key is used; just the curly brackets.
  • There are no official comments, however, some parsers accept C-like comments (// or /* */).

Simple example

{
  "name": "bob",
  "age": 24,
  "height": 180.2,
  "shoppingList": [1, "Apple", 2, "Oranges"],
  "religulous": false,
  "money": null,
  "backpack": {"bread": 2, "porridge": null}
}

Prettify

Easy prettification:

file.json | python3 -m json.tool