JSON (JavaScript Object Notation) is a lightweight text format designed specifically to move data between different systems.
Think of it like universal packaging: you can put your Python dictionary inside, ship it across the internet in plain text, and unpack it on another machine. It looks almost exactly like how we write dictionaries and lists here.
Think of it like universal packaging: you can put your Python dictionary inside, ship it across the internet in plain text, and unpack it on another machine. It looks almost exactly like how we write dictionaries and lists here.
Why use JSON?
You will see this everywhere: when a web app talks to its server, or when you save your high scores in a game. It is easy for humans to read and write.
Here is the same data as both JSON text and a Python dictionary:
Here is the same data as both JSON text and a Python dictionary:
{"name": "Alice", "age": 30}{'name': 'Alice', 'age': 30}Turning data into text: dumps()
import json
data = {'name': 'Alice', 'age': 30}
json_text = json.dumps(data)
print(json_text)dumps is short for dump string. It takes your live data and returns it as a string you can save or send.Turning text back into data: loads()
import json
json_text = '{"name": "Alice", "age": 30}'
data = json.loads(json_text)
print(data['name'])loads is short for load string. It takes the string and gives you back an object with keys, values, and types.