Working with JSON in Python
DSCI 551
Wensheng Wu
Python primer
• String: 'abc' or "abc"
• List: x = ['abc', 25]
– x[0] =?
– [Link](True) // True is boolean
– [Link](None) // None is NoneType
• Tuple: y = ('abc', 25)
– y[0] =?
– What about [Link](True)?
2
Python primer
• Dictionary: z = {'name': 'john', 25: 'age'}
– Note key in Python can also be integer or tuple
– z['name'] = ?
– z[25]=?
– What about z['age'] or z['25']?
• z['gender'] = 'male'
– z = {25: 'age', 'name': 'john', 'gender': 'male'}
3
Working with JSON in Python
• import json
– Loading json library module
• [Link]()
– JSON encoder
– Python object => JSON document
• [Link]()
– JSON decoder
– JSON document => Python object
4
Python object => JSON document
• Python list => JSON array
– [Link]([1, 2]) => '[1, 2]'
– [Link]([3, 'abc', True, None]) => '[3, "abc",
true, null]'
• Python tuple => JSON array
– [Link]((1, 'abc')) => '[1, "abc"]'
5
Python object => JSON document
• Python dictionary => JSON object
– [Link]({'name': 'john', 25: 'age'}) => '{"25":
"age", "name": "john"}'
• Notes
– None => null
– True => true
– 'abc' => "abc"
6
Python object => JSON document
• [Link](['foo', {'bar': ('baz', None, 1.0,
2)}])
– '["foo", {"bar": ["baz", null, 1.0, 2]}]'
• [Link]({(1,2): 5})
– Error (key is a tuple, Ok in Python)
– dumps() doesn't take tuple as key (but see below)
• [Link]({(2): 5}) => '{"2": 5}'
7
JSON document => Python object
• JSON object => Python dictionary
• [Link]('{"name": "john", "age": 5}') => {u'age':
5, u'name': u'john'}
• Note: 'u' means "unicode"
• JSON array => Python list
• [Link]('[25, "abc"]') => [25, u'abc']
8
JSON document => Python object
• [Link]('"abc"') => u'abc'
• [Link]('25.2') => 25.2
• [Link]('true') => True
• [Link]('null') => None
• [Link]('{"name": "john", "age": 25,
"phone": [123, 456]}')
=> {u'phone': [123, 456], u'age': 25, u'name':
u'john'}
9
Conversion summary
JSON Python
Object Dictionary
Array List
Array Tuple (from
Python)
null None
true True
false False
Python dictionary => JSON object
• Keys in Python can be number, string, or tuple.
• Number is also converted to string.
• But tuple (with two or more components) is not acceptable by dumps()/dump().
10
Working with files
• f = open('[Link]')
• lax= [Link](f)
• out_file = open('[Link]', 'w')
• [Link](lax, out_file)
11
LAX passenger traffic data
• Download data at:
– [Link]
Angeles-International-Airport-Passenger-
Traffi/g3qu-7q2u
• A copy is available on Blackboard too
– In the Resources folder
12
Data spreadsheet
13
JSON file ([Link])
• Ignore "meta" info (in the beginning of file)
• Records are in the value of "data"
14
Querying it in Python
ReportPeriod
15
Resources
• JSON
– [Link]
– Syntax: [Link]
• JSON encoder and decoder
– [Link]
16