Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 JSON Data Parsing

Python3 JSON Data Parsing

JSON (JavaScript Object Notation) is a lightweight data interchange format.

If you are not yet familiar with JSON, you can first read our JSON Tutorial。

In Python3 the json module can be used to encode and decode JSON data; it contains two functions:

During JSON encoding and decoding, Python's native types and JSON types are converted into each other; the specific mapping is as follows:

Table for converting Python types to JSON types when encoding:

Python JSON
dict object
list, tuple array
str string
int, float, int- & float-derived Enums number
True true
False false
None null

Table for converting JSON types to Python types when decoding:

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

json.dumps and json.loads examples

The following example demonstrates converting a Python data structure to JSON:

Example (Python 3.0+)

#!/usr/bin/python3

import

json

Convert a Python dictionary to a JSON object

data

= {

'

no

'

:

1

,

'

name

'

:

'

Runoob

'

,

'

url

'

:

'

https://www.runoob.com

'

}

json_str

=

json

.

dumps

(

data

)

print

(

"

Original Python data:

"

,

repr

(

data

)

)

print

(

"

JSON object:

"

,

json_str

)

The output of the code above is:


Python 原始数据: {'no': 1, 'name': 'Runoob', 'url': 'https://www.runoob.com'}
JSON 对象: {"no": 1, "name": "Runoob", "url": "https://www.runoob.com"}

As the output shows, after encoding, simple types look very similar to their original repr() output.

Continuing the above example, we can convert a JSON-encoded string back into a Python data structure:

Example (Python 3.0+)

#!/usr/bin/python3

import

json

Convert a Python dictionary to a JSON object

data1

= {

'

no

'

:

1

,

'

name

'

:

'

Runoob

'

,

'

url

'

:

'

http://www.runoob.com

'

}

json_str

=

json

.

dumps

(

data1

)

print

(

"

Original Python data:

"

,

repr

(

data1

)

)

print

(

"

JSON object:

"

,

json_str

)

Convert a JSON object into a Python dictionary

data2

=

json

.

loads

(

json_str

)

print

(

"

data2['name']:

"

,

data2

[

'

name

'

]

)

print

(

"

data2['url']:

"

,

data2

[

'

url

'

]

)

The output of the code above is:


Python 原始数据: {'name': 'Runoob', 'no': 1, 'url': 'http://www.runoob.com'}
JSON 对象: {"name": "Runoob", "no": 1, "url": "http://www.runoob.com"}
data2['name']:  Runoob
data2['url']:  http://www.runoob.com

If you are dealing with files rather than strings, you can use json.dump() and json.load() to encode and decode JSON data. For example:

Example (Python 3.0+)

Write JSON data

with

open

(

'

data.json

'

,

'

w

'

)

as

f

:

json

.

dump

(

data

,

f

)

Read data

with

open

(

'

data.json

'

,

'

r

'

)

as

f

:

data

=

json

.

load

(

f

)

For more information, see:https://docs.python.org/3/library/json.html