CS计算机代考程序代写 python Working with JSON in Python

Working with JSON in Python

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

10

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().

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

https://data.lacity.org/A-Prosperous-City/Los-Angeles-International-Airport-Passenger-Traffi/g3qu-7q2u

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

15

ReportPeriod

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

https://en.wikipedia.org/wiki/JSON
http://www.json.org/
https://docs.python.org/2/library/json.html