- Python
- Storage
- Files
- Databases
- Pipes
- With Open
- Shelve
- Zip
- Csv
- Json ♣
- Class
- Definition
- Attributes
- Functional
- Methods
- Goodies
- Conditional Expression
- List Comprehension
- Generator
- Named Tuple
- Modules
- Applications
- Pythagora
- Palindrome
- Word Search
- Conway Game
- Coin Flip
- Strings
- Regex
- Encrypt
- Scheduler
- Time
- Multithreading
- Subprocess
- Logging
- Packages
- Clipboard
- Ocr
- Socket
- Image
- Virtualenv
- Jupyter
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}'