JSON in Python (P1)

Tram Ho

1. What is JSON?

JSON is a data format that uses plain text, JSON format uses key-value pairs for the data to use. JSON was originally developed for use in JavaScript applications. The JSON term itself stands for JavaScript Object Notation. However, since JSON is a data format, it can be used by any language without limitation with JavaScript.

2. JSON in Python

Python has a built-in caller called json that can be used to work with JSON data.

1. Convert JSON in Python

Use the json.loads() function to convert a JSON string to Dictionary

2. Convert the Python object to JSON

Use the json.dumps() function to convert a Python Object into a JSON string

The list of objects can be converted to a JSON string

  • dict
  • list
  • tuple
  • string
  • int
  • float
  • True
  • False
  • None

Object conversion table between Python and JSON

PythonJSON
dictObject
listArray
tupleArray
strString
intNumber
floatNumber
Truetrue
Falsefalse
Nonenull

3. Format display

For a nice display, it is easier to see json.dumps() provides an additional indent parameter to format the indent number (1 indent equals 1 space)

4. Arrange the key

By default JSON will display the keys out of order. To sort keys, json.dumps() provides the sort_keys parameter

3. Conclusion

The json module in Python also integrates many other features in the above methods. The next part I will provide more for you. Source: https://www.w3schools.com/python/python_json.asp

Share the news now

Source : Viblo