JSON Module
JSON Format
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
JSON is built on two structures:
- A collection of name/value pairs.
- An ordered list of values.
An OBJECT is an unordered set of name/value pairs.
An object begins with {
(left brace) and ends with }
(right brace).
Each name is followed by :
(colon) and the name/value pairs are separated by ,
(comma).
An ARRAY is an ordered collection of values.
An array begins with [
(left bracket) and ends with ]
(right bracket).
Values are separated by ,
(comma).
A VALUE can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
A STRING is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A CHARACTER is represented as a single character string. A STRING is very much like a C or Java string. A NUMBER is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
WHITESPACE can be inserted between any pair of tokens.
Usage
Default Decoder (json.JSONDecoder()
)
Conversions (JSON -> Python):
- object -> dict
- array -> list
- string -> str
- number (int) -> int
- number (real) -> float
- true -> True
- false -> False
- null -> None
Default Encoder (json.JSONEncoder()
)
Conversions (Python -> Json):
- dict -> object
- list, tuple -> array
- str -> string
- int, float, Enums -> number
- True -> true
- False -> false
- None -> null
Extending JSONEncoder (Example)
Python | |
---|---|