Working with JSON
JSON
JSON = JavaScript Object Notation → Pronounced like the first name 'Jason'
- Language-independent and lightweight data format for data exchange between servers and (web) applications
- Derived from JavaScript
- Used for storing and transporting data
- Designed to be ‘human-readable’
- REST very often works with JSON
- File format: .json
JSON is made up by key-value pairs
- A key is always a string, enclosed by double quotes
- A semicolon (:) seperates the key and value
- A value must be one of the following data types:
- A string
- A number
- An object
- An array
- A boolean
- null
- Pay attention: string values must be enclosed by double quotes
{
"course":"API Development"
}
{
"key":value
}
A JSON file usually contains multiple key-value pairs
- Seperated by a comma
- No comma after the last key-value pair
Correct: key-value pairs seperated by a comma
{
"course":"API Development",
"semester":1
}
The order of key-value pairs doesn't matter
- For JSON, both objects/files (above and below this text) contain the same data
Correct: order of key-value pairs changed
{
"semester":1,
"course":"API Development"
}
A key can only occur once within an object, but can occur again in child objects
Wrong: the same key twice within the same object
{
"course":"API Development",
"course":"Python"
}
Notation of data types
- A string
- A number
- An object
- An array
- A boolean
- null
{
"string":"a string value",
"wholeNumber":17,
"decimalNumber":7.37,
"object":{
"course":"API Development",
"hoursPerWeek":3
},
"array":[
{"name":"Els Depaepe", "class":"2 CCS 01"},
{"name":"Simon Vervoort", "class":"2 CCS 02"},
{"name":"Bram Verbiest","class":"2 IOT"}
],
"boolean":true,
"empty":null
}
Example 1: from table to JSON
Table: results
course | student | grade |
---|---|---|
Python | Laurien Stas | 12 |
SQL | Robbe Van Amont | 4 |
Python | Michaël Aerts | 14 |
{
"results":[
{
"course":"Python",
"student":"Laurien Stas",
"grade":12
},
{
"course":"SQL",
"student":"Robbe Van Amont",
"grade":4
},
{
"course":"Python",
"student":"Michaël Aerts",
"grade":14
}
]
}
Example 2: nested structure
Table: course
courseId | name | semester |
---|---|---|
1 | Python | 1 |
2 | SQL | 2 |
Table: result
resultId | courseId | student | grade |
---|---|---|---|
1 | 1 | Laurien Stas | 12 |
2 | 2 | Robbe Van Amont | 4 |
3 | 1 | Michaël Aerts | 14 |
{
"courses":[
{
"courseId":1,
"name":"Python",
"semester":1,
"results":[
{"resultId":1,"student":"Laurien Stas","grade":12},
{"resultId":3,"student":"Michaël Aerts","grade":14}
]
},
{
"courseId":2,
"name":"SQL",
"semester":2,
"results":[
{"resultId":2,"student":"Robbe Van Amont", "grade":4}
]
}
]
}
The structure of JSON should always be correct, otherwise it can not be parsed
Check whether the structure is correct or not: https://jsonformatter.curiousconcept.com/
Enter your JSON and click on 'Process'
Scroll down and check the validity
Made a mistake? Check the error message(s)!
Exercise: convert the following table data into JSON
Table: song
name | artist | durationInSeconds | hasAlbum |
---|---|---|---|
Something | Lasgo | 221 | true |
Phoenix | Enemy Inside | 208 | true |
Expectations | Lauren Jauregui | 204 | false |
JSON in Python
Python has a build in package to work with JSON data
To use it, import the 'json' package:
import json
The structure of JSON is very similar to a dictionary in Python → Time for a small reminder!
Dictionary
- A dictionary is an ordered, changeable collection of key-value pairs
- The key is unique so you can use it to retrieve any value
- You can imagine a dictionary as a table with two columns
letters = {'e': 1, 'h': 1, 'n': 1, 'o': 2, 't': 4}
A dictionary is created via curly braces: {}
Key and value are separated by a :
Key/value pairs are separated by a ,
dictionary = {key 1: value 1, ..., key n: value n}
Looking up a value is done by the key
First method
The key is given 'as an index' in square brackets
letters = {'e': 1, 'h': 1, 'n': 1, 'o': 2, 't': 4}
print('Occurrences of letter t:', letters['t'])
Second method
A dictionary also has a method to retrieve the value associated with a key.
The advantage of the method get()is that you can decide what is returned if the key is not found.
letters = {'e': 1, 'h': 1, 'n': 1, 'o': 2, 't': 4}
my_choice = input('Letter? ')
print(letters.get(my_choice, 'This character does not occur'))
Parsing JSON
json.loads()
- Function that can be used to parse a valid JSON string and convert it into a Python dictionary
- Expects a string value as parameter, not a file
- Useful if you already have a JSON object in Python
From JSON to Python dictionary
import json
# a JSON course object, put in a Python variable:
json_course = '{"name":"API Development","semester":1}'
# parse the JSON object using the loads() function:
course = json.loads(json_course)
# print the data:
print("Course name:", course["name"])
print("Semester:", course["semester"])
json.load()
- Function that can be used to parse the data from a .json file to a JSON object
- Then converts the JSON object into a Python dictionary
- Expects a file location as parameter
From JSON file to Python dictionary
course.json
{
"name":"API Development",
"semester":1
}
import json
with open("course.json") as json_file:
# read out the file and parse the JSON object using the load() function:
course = json.load(json_file)
print(course)
# print the data:
print("Course name:", course["name"])
print("Semester:", course["semester"])
json.dumps()
- Function that can be used to convert a Python dictionary (or other object) into a JSON string
- Expects a dictionary or other Python object as parameter
From Python dictionary to JSON
import json
# a Python dictionary:
course = {'name': 'API Development', 'semester': 1}
# convert into JSON using the dumps function:
json_course = json.dumps(course)
# the result is a JSON string:
print(json_course)
json.dump()
- Function that can be used to write data of a Python dictionary to a .json file
- Writes in the memory and then the command for writing to disk is executed separately
- Expects 2 parameters: first the Python dictionary, then the file to write to
- Pay attention:
- The file should be opened before the json.dump() function is called
- The file should be closed after calling the json.dump() function
From Python dictionary to JSON file
import json
# a Python dictionary:
course = {'name': 'API Development', 'semester': 1}
with open("course.json", "w", encoding="UTF-8") as file:
json.dump(course, file)
Indent
- A big advantage of JSON is readability
- By default, Python generates one line in a .json file
- Solution to make it even more readable: add indents
import json
# a Python dictionary:
course = {'name': 'API Development', 'semester': 1}
with open("course.json", "w", encoding="UTF-8") as file:
json.dump(course, file, indent=3)