2.18.2021

Implement with Python serialize objects with JSON

 The json module exposes two methods for serializing Python objects into JSON format.

dump() will write Python data to a file-like object. We use this when we want to serialize our Python data to an external JSON file.

dumps() will write Python data to a string in JSON format. This is useful if we want to use the JSON elsewhere in our program, or if we just want to print it to the console to check that it’s correct.

Python and JSON do not share all the same types. Serialization will convert your Python objects into JSON format according to this table

PythonJSON
dictobject
list, tuplearray
strstring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull

Both the dump() and dumps() methods allow us to specify an optional indent argument. This will change how many spaces is used for indentation, which can make our JSON easier to read.

json_str = json.dumps(data, indent=4)

No comments:

Node.js?

  Node.js? It is JS or Javascript yes, it is the basis.  Yes the old Javascript that today has a strong front and back end stack, the same s...