JSON

Following on from this post, I thought it might be good to think about JavaScript Object Notation (JSON).

Despite its name, JSON has evolved to be agnostic and is arguably the standard for exchanging information because its human readable and easy for computers to parse.

JSON is two things: a collection of key value pairs, and an ordered list of values. The former is easily represented with objects, dictionaries, etc. and he latter with arrays, lists, or sequences.

  • Values are string, number, object, array, true/false, or null surrounded by white-space.
  • Objects are unordered key-value pairs, represented by colon separated key-value pairs and wrapped in curly brackets.
  • Arrays are comma separated ordered collections wrapped in square brackets.
  • Comments, trailing commas, and single quoted strings are not supported.
{
    "name" : "Dougie",
    "languages" : [ "python", "java" ]
}

Python has a standard module, json, making it simple to convert Python and JSON data types. Serialisation is converting to JSON, but take care with de-serialisation as it is not quite the reverse. Not all Python data types can be converted to JSON values, so objects may not retain their original type.

This can introduce subtle problems when serialising and de-serialising:

  • JSON requires keys to be strings, so where another type is given during serialisation, it will be a string when de-serialised.
  • True serialises to true, and None serialises to null. The reverse is also true.
  • Tuple are serialised as arrays, there is no way to tell when de-serialising so you will get a list.

Serialising is done by dumping:

json.dumps(python_data)

Deserialising JSON is done by loading:

json.loads(json_data)

JSON is human readable but can be made easier to read by specifying indentation level:

json.dumps(sample_json, indent=2)

Posted

in

,

by

tagged: