Search Header Logo
Json with Python

Json with Python

Assessment

Presentation

•

Computers

•

Professional Development

•

Hard

Created by

Jeff Thuong

Used 4+ times

FREE Resource

9 Slides • 6 Questions

1

Manipulation of JSON with Python

Some text here about the topic of discussion

2

media

Remember what is JSON?​

No problem, here is what you need to know​

​Reminder about JSON

JSON x Python

3

In JSON, values must be:

  • a string

  • a number

  • an object (JSON object)

  • an array

  • a boolean

  • null

JSON values cannot be one of the following data types:

  • a function

  • a date

  • undefined

JSON x Python

Data is stored as dictionary with the key being a string​

{

    "a string": "This is a string",

    "a number": 1,

    "a_boolean": true,

    "another_boolean": false,

    "nothing": null,

    "an array": [1, 2, 3],

    "an object": {

        "name": "Jeff",

        "age": 99​

    }

}​

​

4

​import json

JSON in Python Standard Library🎉

You can write (dump / dumps)

and read (load / loads)​

JSON x Python

JSON x Python

5

​dumps (... with a -s)

json.dump(obj, fp, *, skipkeys=False, ...)​

Dump a JSON string of an object to an open file​​​

​

with open("data.json", "w") as f:

    json.dump(my_data, ​f)

​dump

Writing

JSON x Python

json.dumps(obj, *, skipkeys=False, ...)​

Dump a JSON string of an object to a string

​

json_repr = json.dumps(my_data)​

6

​loads (... with a -s)

json.load(fp, *, ...)​

Load JSON data from open file​​​

​

with open("data.json") as f:

    data = json.load(​f)

​load

Reading

JSON x Python

json.loads(s, *, ...)​

Load JSON data from a string

​

data = json.loads(json_repr)​

7

Multiple Choice

Which function can READ data from

a json file opened ("with open(...) as f: ...")

1

json.read

2

json.load

3

json.dumps

4

json.loads

8

Multiple Choice

Which function can READ data from

a json stored in a string (json_string = '{"name": "jeff"}')

1

json.read

2

json.load

3

json.dumps

4

json.loads

9

Multiple Choice

Which code DOES NOT write JSON

representation of a data (e.g. a dictionary)

1

with open("data.json") as f:

json.dump(data, f)

2

with open("data.json", "w") as f: f.write(json.dumps(data))

3

f = open("data.json", "w")

json.dump(data, f)

10

media

Open VS Code, create a Jupyter Notebook and start dumping / loading JSON data

Now your turn!

11

Multiple Choice

Which data CANNOT BE dumped to JSON?

1

None

2

List

3

Dictionary

4

Tuple, like: (1, 2, 3)\left(1,\ 2,\ 3\right)  

5

Dates (datetime.datetime object)

12

Multiple Select

Which data would be different after being dumped to JSON and loaded

(SEVERAL ANSWERS POSSIBLE)?

1

None

2

Dictionary with keys different from strings

3

Lists with a mix of integers and strings

4

Tuple, like: (1, 2, 3)\left(1,\ 2,\ 3\right)  

13

​We cannot dump this type of data in JSON

​Date and Path => Error

JSON x Python

media

14

​We can use a serializer and pass it as "default" argument.

... but data will still be a string after loading​

​BONUS: dumping dates

JSON x Python

media

15

Poll

How much do you understand handling of JSON with Python now?

Very Good

Good

So-so

Still confused

pattern-tertiary
Manipulation of JSON with Python

Some text here about the topic of discussion

Show answer

Auto Play

Slide 1 / 15

SLIDE