✦ Python Foundations Working with Data module

Concept 21 · JSON Data

Turn text into data.
And data back into text.

JSON is a common text format for structured data. json.loads() parses JSON text into Python data; json.dumps() creates JSON text from Python data.

01 Learn02 Try03 Predict

JSON text can be converted to Python dictionaries and lists.

JSON is a common text format for structured data. json.loads() parses JSON text into Python data; json.dumps() creates JSON text from Python data.

import json

student_json = '{"name": "Ana", "score": 95}'
student = json.loads(student_json)
print(student["name"])

result = json.dumps(student)
print(result)

student_json is text, while student is a Python dictionary after loads(). dumps() makes JSON text again.

Ana {"name": "Ana", "score": 95}

Change one value, then explain how the resulting data changes.

↳

Keep this in mind: Follow each data operation in order before predicting the result.

Try it
import json

student_json = '{"name": "Ana", "score": 95}'
student = json.loads(student_json)

print(student["name"])
student["score"] += 5
print(json.dumps(student))
# Change the JSON text or read another key

Run the example, then change one value and observe the result.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

json_data.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each data operation before choosing an answer.

import json

course = json.loads('{"title": "Python", "students": 12}')
print(course["title"])
print(course["students"] + 1)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Ask Sloby about JSON data

Describe what feels unclear and Sloby will guide your reasoning.

Hi! I'm Sloby. Let's turn JSON text into useful Python data.

What will this program print, and what is the difference between json.loads() and json.dumps()?

import json

data = json.loads('{"name": "Ana", "scores": [8, 9]}')
data["scores"].append(10)

print(data["name"])
print(len(data["scores"]))
print(json.dumps(data))
Python AI Tutor · Learn by understanding, not just by running code.