JSON, explained simply
Programs often need to pass information to each other: a game saves its progress, a weather app downloads today’s forecast. JSON is a common text format for doing that.
JSON looks almost exactly like Python dictionaries and lists, which makes it easy to learn. The json module converts between the two.
json.dumps() turns a Python value into a JSON string, and json.loads() turns a JSON string back into Python. json.dump() and json.load() do the same directly with files.
Example: Pack a message
import json
explorer = {"name": "Meera", "badges": 4}
text = json.dumps(explorer)
print(text)
back = json.loads(text)
print(back["badges"] + 1)Output
{"name": "Meera", "badges": 4}
5Try this
- Save a dictionary of your favourite things to a .json file with json.dump.
- Write JSON by hand as a string and load it into a dictionary.
- Look at the difference between True in Python and true in the JSON text.
