Explanation of encoding and decoding method of Python3 json module

  • 2021-10-24 23:14:13
  • OfStack

JSON (JavaScript Object Notation) is a lightweight data exchange format based on a subset of ECMAScript. JSON adopts a text format completely independent of language. These characteristics make JSON an ideal data exchange format, which is easy for people to read and write, and also easy for machine analysis and generation. It is very common in interface data development and transmission.

In Python3 we use the built-in module json to decode and encode JSON objects. The json module provides four functions:

dumps , dump , loads , load

dumps Converts Data Types to Strings

dump converts data types into strings and stores them in files

loads Converts Strings to Data Types

load converts file opening from string to data type

dumps coding

We use dumps to encode Python object into JSON object. Of course, dumps only completes serialization into str, while dump must transmit file descriptor and save serialized str to file.

Coding dictionary


import json 
odata = {'www': 1, 'ofstack.com': 2, 'Python3': 3}
jdata = json.dumps(odata)
print(jdata)

Example results:

{"www": 1, "ofstack.com": 2, "Python3": 3}

Coded list


import json 
ldata = [100, 'Python2', {'www': 1, 'ofstack.com': 2, 'Python3': 3}]
jdata = json.dumps(ldata)
print(jdata)

Example results:

[100, "Python3", {"www": 1, "ofstack.com": 2, "Python3": 3}]

Coded string


import json 
sdata = 'Python3'
jdata = json.dumps(sdata)
print(jdata)

Example results:

"Python3"

Formatted Output JSON

Convert the following array to the standard json format


import json 
ldata = ['Python3', 100, {'www': 1, 'ofstack.com': 2, 'Python3': 3}, True]
jdata = json.dumps(ldata, sort_keys=True, indent=4)
print(jdata)

Example results:


[
 "Python3",
 100,
 {
  "Python3": 3,
  "ofstack.com": 2,
  "www": 1
 },
 true
]

Parameter resolution:

sort_keys=True Then the output of the dictionary will be sorted in key order

indent=4 Indicates an indentation of 4. If indent is a non-negative integer or string, JSON array elements and object members are beautified and output to the indentation level specified for that value.

Conversion relation comparison table

The following is a comparison table for converting Python primitive types to JSON objects:

Python JSON
dict object
list, tuple array
str, unicode string
int, long, float number
True true
False false
None null

loads decoding

We use loads to decode JSON objects. The decoding result is the corresponding Python object type. Of course, loads only completes deserialization, and load only receives file descriptors, reads files and deserializes them.

For example, we used to decode the data in the previous example


import json 
jsondata = '''
[
 "Python3",
 100,
 {
  "Python3": 3,
  "ofstack.com": 2,
  "www": 1
 },
 true
]
'''
ldata = json.loads(jsondata)
print(type(ldata))
print(ldata)

Example results:

< class 'list' >

['Python3', 100, {'Python3': 3, 'ofstack.com': 2, 'www': 1}, True]

It can be seen that we successfully decoded the JSON object in the previous example, and the final decoding result is Python list object type, which conforms to the result of Python object JSON object comparison table.

Summarize

json parsing and coding module json in Python3 is very simple to use and has complete functions, which can fully meet our daily needs. For more articles on Python json module parsing and coding, please click on the relevant links below


Related articles: