- BASICS
- Statements
- Operators
- Functions
- Incremental
- Errors
- FUNCTIONS
- Recursion
- Objects
- Lambda
- STRINGS
- Immutable
- Raw Strings
- Validation
- Config
- Security
- CLASS
- Definition
- Attributes
- Functional
- Methods
- COLLECTIONS
- Lists
- Dictionaries
- Efficiency
- Tree
- Iterator
- Tuples
- References
- STORAGE
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
-
Json
Read
Python json module translates json to Python values.
"""Json read:
Json string always uses double quotes.
The loads() methods return json as a Python dictionary.
The loads() method means 'load string' not 'loads'
"""
import json
str = '{"name":"John", "age":40, "job":null}'
dict = json.loads(str)
assert dict.get('name') == 'John'
assert dict.get('age') == 40
assert dict.get('job') == None
Write
The dump() method translates Python value into a json string.
"""Json write:
The method dumps() mean 'dump string' not 'dumps'
"""
import json
dict = {"name":"John", "age":40, "job":None}
json = json.dumps(dict)
assert json == '{"name": "John", "age": 40, "job": null}'
assert json != {"name": "John", "age": 40, "job": None}
assert json != '{"name": "John", "age": 40, "job": None}'
Last update: 395 days ago