Python: How to read and write JSON

JSON is a lightweight data-interchange format that stands for JavaScript Object Notation. It so happens that JavaScript objects look exactly like Python dictionaries, so JSON objects can be thought of as portable dictionaries of key-value pairs. This portability is what makes JSON so valuable.

While JSON objects can be identical to Python dictionaries, there are some caveats:

JSON object keys and values must all be strings, and JavaScript uses double quotes to denote strings, so Python dictionaries that use single quotes for strings would be invalid JSON. If we want to convert a Python dictionary to a JSON object, then the single quotes would need to be converted to double quotes. In fact, this is exactly what the Python json module does when using the dumps() function. The ‘s’ stands for strings.

For example, lets say we have a Python dictionary called fruit that looks like this:

{'a':'apple', 'b':'banana', 'c':'cherry'}

When we convert this to a JSON object using dumps(), the string representation is changed to be more JavaScript friendly.

{"a": "apple", "c": "cherry", "b": "banana"}

Python dictionary to JSON:

Here is the code that makes the conversion from Python dictionary to JSON. Notice it uses dumps(python_dict). The argument is the Python dict to be converted:

import json

# Python dict
fruit =  {'a':'apple', 'b':'banana', 'c':'cherry'}  

# Use the json module to convert the Python dict to a JSON object using .dumps()
fruit_to_json = json.dumps(fruit)

print fruit_to_json

Just as Python dictionaries support multiple data types for values, JSON objects support key-value pairs where the values are either a list of strings or a sub-dictionary of key value pairs. For example, this is valid JSON:

{
	"a": {"apple": {"type": "red delicious"}},
	"b": "banana",
	"c": ["cherry", "cantaloupe"]
}

Converting this Python dictionary to JSON and printing it out:

import json

# Python dict
fruit = {
	"a": {"apple": {"type": "red delicious"}},
	"b": "banana",
	"c": ["cherry", "cantaloupe"]
}

# Use the json module to convert the Python dict to a JSON object using .dumps()
fj = json.dumps(fruit)
print fj
print type(fj)

Yields:

{"a": {"apple": {"type": "red delicious"}}, "c": ["cherry", "cantaloupe"], "b": "banana"}
<type 'str'>

Note: the json.dumps() function supports an argument called ‘indent’ which will display the JSON object differently. For example:

fj = json.dumps(fruit, indent=4)
print fj

Which yields:

{
    "a": {
        "apple": {
            "type": "red delicious"
        }
    }, 
    "c": [
        "cherry", 
        "cantaloupe"
    ], 
    "b": "banana"
}

JSON to Python dictionary:

Now that I’ve demonstrated how to convert a Python dictionary to a JSON object using the  json.dumps() function, here is how we can do the reverse. To load a JSON object into a Python dictionary, we need to use the  json.loads() function. (‘s’ stands for string). Remember that JSON objects are always transported as strings. For example:

import json

# JSON object is transported as a string (dictionary with quotes around it)
json_fruit = '{"a": {"apple": {"type": "red delicious"}}, "c": ["cherry", "cantaloupe"], "b": "banana"}'

print type(json_fruit)      # type is str

# json.loads() converts the JSON string into a Python dict
python_fruit = json.loads(json_fruit)

print type(python_fruit)    # type is dict

Which yields:

<type 'str'>
<type 'dict'>

In summary, JSON objects are transported as strings which can easily be converted to Python dictionaries using the  json.loads() function. And Python dictionaries can be converted to JSON strings using the  json.dumps() function.

Note: Sometimes you will need to load JSON from a file or a website instead of from a string. In those cases, use  json.load() (without the ‘s’). For example:

import json
json_data = urllib2.urlopen('http://site.com/data.json')
json_obj = json.load(json_data)

For more information about the Python JSON module, see:
https://docs.python.org/2/library/json.html

If you need to validate your JSON, there is an online tool called JSONlint:
https://jsonlint.com/

Leave a Reply