Python object JSON document jsondumpsfoo bar baz None 10 2 foo bar baz null 10

Python object json document jsondumpsfoo bar baz none

This preview shows page 7 - 16 out of 16 pages.

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
Image of page 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
Image of page 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
Image of page 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().
Image of page 10
Working with files f = open('lax.json') lax= json.load(f) out_file = open('output.json', 'w') json.dump(lax, out_file) 11
Image of page 11
LAX passenger traffic data Download data at: - Angeles-International-Airport-Passenger- Traffi/g3qu-7q2u A copy is available on Blackboard too In the Resources folder 12
Image of page 12
Data spreadsheet 13
Image of page 13
JSON file (lax.json) Ignore "meta" info (in the beginning of file) Records are in the value of "data" 14
Image of page 14
Querying it in Python 15 ReportPeriod
Image of page 15
Resources JSON Syntax: JSON encoder and decoder 16
Image of page 16

You've reached the end of your free preview.

Want to read all 16 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture