minte9
LearnRemember



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: 315 days ago