- BASICS
- Statements
- Operators
- Functions
- Incremental
- Errors
- FUNCTIONS
- Function Definition
- Recursion
- Objects
- STRINGS
- Immutable
- Raw Strings
- Regex
- Validation
- Config
- Security
- Encrypt
- CLASS
- Definition
- Attributes
- Functional
- Methods
- COLLECTIONS
- Lists
- List Comprehension
- Dictionaries
- Dictionary Efficiency
- Tuples
- References
- Iterable
- STORAGE
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
-
Json
PYTHON PAGES - LEVEL 1
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}'
Questions and answers:
Clink on Option to Answer
1. json.loads() means:
- a) load string
- b) loads