Tutorials
python

JSON Data in Python

In this tutorial, you'll learn how to use JSON in Python.

JSON(JavaScript Object Notation) is a lightweight data-interchange format that easy for humans to read and write. It is also easy for computers to parse and generate. JSON is based on the JavaScript programming language. It is a text format that is language independent and can be used in Python, Perl among other languages. It is primarily used to transmit data between a server and web applications. JSON is built on two structures:

• A collection of name/value pairs. This is realized as an object, record, dictionary, hash table, keyed list, or associative array.
• An ordered list of values. This is realized as an array, vector, list, or sequence.

JSON in Python

There are a couple of packages that support JSON in Python such as metamagic.json, jyson, simplejson, Yajl-Py, ultrajson, and json. In this tutorial, we'll use json which is natively supported by Python. We can use this site that provides a JSON linter to verify our JSON data.

Below is an example of JSON data. We notice that the data representation is very similar to Python dictionaries.

{
"article": [

{
"id":"01",
"language": "JSON",
"edition": "first",
"author": "Derrick Mwiti"
},

{
"id":"02",
"language": "Python",
"edition": "second",
"author": "Derrick Mwiti"
}
],

"blog":[
{
"name": "Datacamp",
"URL":"datacamp.com"
}
]
}


Converting JSON to Python Objects

We can parse the above JSON string using json.loads() method from the json module. The result is a Python dictionary.

import json
my_json_string = """{
"article": [

{
"id":"01",
"language": "JSON",
"edition": "first",
"author": "Derrick Mwiti"
},

{
"id":"02",
"language": "Python",
"edition": "second",
"author": "Derrick Mwiti"
}
],

"blog":[
{
"name": "Datacamp",
"URL":"datacamp.com"
}
]
}
"""

to_python['blog']

[{'URL': 'datacamp.com', 'name': 'Datacamp'}]


Converting Python Objects to JSON

Using json.dumps() we can convert Python Objects to JSON.

blog = {'URL': 'datacamp.com', 'name': 'Datacamp'}
to_json= json.dumps(blog)

to_json

'{"URL": "datacamp.com", "name": "Datacamp"}'


Let's compare the data types in Python and JSON.

Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null

Below we'll show how you can convert various Python objects to different JSON data types.

Python Tuple to JSON Array

tuple_example = 'Mango', 'Banana', 'Apple';
print(json.dumps(tuple_example));

["Mango", "Banana", "Apple"]


Python List to JSON Array

list_example = ["Mango", 1, 3,6, "Oranges"];
print(json.dumps(list_example));

["Mango", 1, 3, 6, "Oranges"]


Python String to JSON String

string_example = "This is a cool example."
print(json.dumps(string_example))

"This is a cool example."


Python Boolean Values to JSON Boolean Values

boolean_value = False
print(json.dumps(boolean_value))

false


Writing a JSON File

The json module also allows us to write JSON data into a JSON file. JSON files are saved with the .json extension. Let's see how we can do that below. In order to achieve this, we use Python's open() function with w as the parameter to signify that we want to write the file.

my_json_string = """{
"article": [

{
"id":"01",
"language": "JSON",
"edition": "first",
"author": "Derrick Mwiti"
},

{
"id":"02",
"language": "Python",
"edition": "second",
"author": "Derrick Mwiti"
}
],

"blog":[
{
"name": "Datacamp",
"URL":"datacamp.com"
}
]
}
"""
with open('test_file.json', 'w') as file:
json.dump(my_json_string, file)


Now let's show how we can read in the JSON file we just created. We use json.load to load in files.

with open('test_file.json', 'r') as j:
print(json_data)

{
"article": [

{
"id":"01",
"language": "JSON",
"edition": "first",
"author": "Derrick Mwiti"
},

{
"id":"02",
"language": "Python",
"edition": "second",
"author": "Derrick Mwiti"
}
],

"blog":[
{
"name": "Datacamp",
"URL":"datacamp.com"
}
]
}


json.load vs json.loads

json.load is used when loading a file while json.loads(load string) is used when loading a string.

json.dump vs json.dumps

We use json.dump when we want to dump JSON into a file. json.dumps(dump string) is used when we need the JSON data as a string for parsing or printing.

Handling JSON Data in Data Science

Sometimes we need to load in data that is in JSON format during our data science activities. Pandas provides .read_json that enables us to do this. Once the data is loaded, we convert it into a dataframe using the pandas.DataFrame attribute.

import pandas as pd
df = pd.DataFrame(data)

df


Implementation Limitations

The process of encoding JSON is called Serialization while the process of decoding JSON is called deserialization. Some JSON deserializer implementations may set limits on:

• The size of accepted JSON texts
• The maximum level of nesting of JSON objects and arrays
• The range and precision of JSON numbers
• The content and maximum length of JSON strings

However such limitations are only those relevant to Python data types and the Python interpreter itself.

JSON in APIs

One of the major applications of JSON is in building APIs in web applications. This is very useful because it allows a fellow developer to build on top of our APIs using any language that supports JSON. Most modern programming languages support JSON. We'll show a simple example of how you can return JSON when building a Flask application in Python. Flask provides the jsonify module that will enable us to achieve this.

from flask import jsonify

@app.route('/_get_current_user')
def get_current_user():
email=g.user.email,
id=g.user.id)


This will send a JSON response to the browser that is similar to the one shown below.

{

{'email': 'myemail@gmail.com', 'id': 1, 'username': 'Derrick Mwiti'}

In this tutorial, we have done an introduction to JSON in Python. We've covered various methods provided by the JSON module such as json.load and json.dumps. We've also seen our we can load JSON data in our data science projects as well as how to return JSON data when building APIs. You can learn more about the JSON module by visiting its official page on the Python website.