As I’ve been messing about with coding some apps to interact with APIs I’ve found myself needing to look at the raw JSON output a fair few times, to try and understand what the format of the data is and which bit I need to extract. JSON is JavaScript Object Notation, basically a simple way of storing text data in a way that can be easily manipulated by computer. Despite the name, it is used all over the web whether javascript is the language you are using or not, and APIs quite often return data in this format.

Anyway reading JSON in a plain string is hard work, too many brackets within brackets for my eyes to see clearly, but I found the JSON Parser Online, its a great tool which you can just paste some JSON and it will nicely format it, colouring the brackets, elements and data differently, and allowing you to close and expand brackets and see clearly which elements are nested inside others. It will also warn you of any errors in the syntax. See the difference between reading a minified JSON string and a nicely parsed version:

json_exampleI’m sure various IDEs (Integrated Development Environments, text editors for coding) have the ability to colour code JSON, but being able to expand and line up each section is a bonus which makes it much easier to read – especially if your input is messy or is contracted. If you want to go the other way round, to minify some JSON to remove the whitespaces and economize on space, try httputility.net.

If you are working with Python, it cannot work with JSON directly but it is very easy to convert JSON data into a Python dictionary type. Now you can access the data nested within it as per python dictionary operations. See this example code:

>>> import json
>>>json_string='''{"$schema":"http://json-schema.org/draft-04/schema#","title":"Product","description":"A product from Acme's catalog","type":"object","properties":{"id":{"description":"The unique identifier for a product","type":"integer"},"name":{"description":"Name of the product","type":"string"}},"required":["id","name"]}'''
>>> python_dict_string=json.loads(json_string)
>>> print python_dict_string['properties']['name']['description']
Name of the product

Beware that sometimes the dictionary will be the first (and only) item in a python list so you’ll need to get it with the first index [0] if this is the case (maybe when opening from a file or getting from an http header). That tripped me up a few times and I was wondering why I kept getting errors when accessing the dictionary. You can tell if you print the output because you’ll see square brackets at the very start and end of the string.