0% found this document useful (0 votes)
5 views

Resources - JSON & Python

The document provides an overview of working with JSON in Python, including data types such as strings, lists, tuples, and dictionaries, and how they correspond to JSON structures. It explains the use of the json library for encoding and decoding between Python objects and JSON documents, along with examples of conversions. Additionally, it includes instructions for reading and writing JSON files and resources for further learning.

Uploaded by

Yuan-hsuan Wen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Resources - JSON & Python

The document provides an overview of working with JSON in Python, including data types such as strings, lists, tuples, and dictionaries, and how they correspond to JSON structures. It explains the use of the json library for encoding and decoding between Python objects and JSON documents, along with examples of conversions. Additionally, it includes instructions for reading and writing JSON files and resources for further learning.

Uploaded by

Yuan-hsuan Wen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Working with JSON in Python

DSCI 551
Wensheng Wu
Python primer
• String: 'abc' or "abc"
• List: x = ['abc', 25]
– x[0] =?
– x.append(True) // True is boolean
– x.append(None) // None is NoneType
• Tuple: y = ('abc', 25)
– y[0] =?
– What about y.append(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

• json.dumps()
– JSON encoder
– Python object => JSON document

• json.loads()
– JSON decoder
– JSON document => Python object

4
Python object => JSON document
• Python list => JSON array
– json.dumps([1, 2]) => '[1, 2]'
– json.dumps([3, 'abc', True, None]) => '[3, "abc",
true, null]'

• Python tuple => JSON array


– json.dumps((1, 'abc')) => '[1, "abc"]'

5
Python object => JSON document
• Python dictionary => JSON object
– json.dumps({'name': 'john', 25: 'age'}) => '{"25":
"age", "name": "john"}'

• Notes
– None => null
– True => true
– 'abc' => "abc"

6
Python object => JSON document
• json.dumps(['foo', {'bar': ('baz', None, 1.0,
2)}])
– '["foo", {"bar": ["baz", null, 1.0, 2]}]'

• json.dumps({(1,2): 5})
– Error (key is a tuple, Ok in Python)
– dumps() doesn't take tuple as key (but see below)
• json.dumps({(2): 5}) => '{"2": 5}'

7
JSON document => Python object
• JSON object => Python dictionary
• json.loads('{"name": "john", "age": 5}') => {u'age':
5, u'name': u'john'}
• Note: 'u' means "unicode"

• JSON array => Python list


• json.loads('[25, "abc"]') => [25, u'abc']

8
JSON document => Python object
• json.loads('"abc"') => u'abc'
• json.loads('25.2') => 25.2
• json.loads('true') => True
• json.loads('null') => None

• json.loads('{"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('lax.json')
• lax= json.load(f)

• out_file = open('output.json', 'w')


• json.dump(lax, out_file)

11
LAX passenger traffic data
• Download data at:
– https://data.lacity.org/A-Prosperous-City/Los-
Angeles-International-Airport-Passenger-
Traffi/g3qu-7q2u

• A copy is available on Blackboard too


– In the Resources folder

12
Data spreadsheet

13
JSON file (lax.json)
• Ignore "meta" info (in the beginning of file)
• Records are in the value of "data"

14
Querying it in Python

ReportPeriod

15
Resources
• JSON
– https://en.wikipedia.org/wiki/JSON
– Syntax: http://www.json.org/

• JSON encoder and decoder


– https://docs.python.org/2/library/json.html

16

You might also like