✦ Python Foundations Working with Data module

Concept 21 · JSON Data

Turn text into data.
And data back into text.

Python already works with structured values such as dictionaries and lists. JSON is a common text format for saving or sharing similar structured data between programs.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Translate JSON text into Python data, then back again.

JSON is text with a standard structure. Python cannot use that raw text in exactly the same way as a dictionary or list, so json.loads() parses JSON text into Python data. After you work with the Python data, json.dumps() creates JSON text again.

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 a string containing JSON text. json.loads(student_json) reads that text, recognizes its structure, and returns a Python dictionary. After that, student["name"] works because student is Python data, not raw JSON text.

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

json.loads() means JSON string → Python value. json.dumps() means Python value → JSON string. Neither call is magic: each returns a new value, and this example stores that returned value under a new name.

↳

Use this as your central JSON map: JSON text → json.loads() → Python data → work or change → Python data → json.dumps() → JSON text

Trace JSON text → Python data → JSON text
import json

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

student["score"] = 100
result = json.dumps(student)

First, student_json is a str. loads() parses its JSON text and makes student a Python dict. The dictionary can be read and changed normally, so the score becomes 100. Finally, dumps() returns a new JSON string in result; student is still the Python dictionary.

JSON text is not the same as a Python dictionary
student_json = '{"name": "Ana", "score": 95}'
student = {"name": "Ana", "score": 95}

These values may look similar, but the quotes around the entire first value make it one string containing JSON text. The second value is a Python dictionary. Python treats them as different types, so parse the JSON text before using dictionary keys.

loads() parses JSON text into Python data
student = json.loads(student_json)
print(student["score"])

Parsing means reading structured text and building the matching Python structure. Here the JSON object becomes a dictionary, so Python can look up "score" with the dictionary operation you already know.

dumps() creates a JSON string from Python data
result = json.dumps(student)
print(result)

dumps() takes a Python value such as a dictionary or list and returns JSON text as a string. It does not change the original dictionary; result holds the new JSON string, just as a function can return a new value under a different name.

A small JSON-to-Python type map
JSON object  → Python dict
JSON array   → Python list
JSON string  → Python str
JSON number  → Python int or float
JSON true / false → Python True / False
JSON null    → Python None

This is a practical map, not a list to memorize all at once. The important idea is that loads() turns JSON structure into the Python values you already know how to use.

Nested JSON becomes nested Python data
course_json = '{"title": "Python", "students": ["Ana", "Marko"]}'
course = json.loads(course_json)
print(course["students"][0])

The JSON object becomes the course dictionary, and its students array becomes a Python list. After parsing, read it one familiar step at a time: dictionary key first, then list index.

Why JSON exists in everyday programs
settings_text = json.dumps({"theme": "dark", "font_size": 16})

Programs often exchange structured information, web services send structured data, and applications save simple settings. JSON gives different programs and languages a common text format while Python gives you dictionaries and lists to work with locally.

A practical JSON-reading checklist

  1. Find the starting valueIs it JSON text in a string, or is it already Python data?
  2. Find the transformationDoes the code call json.loads() or json.dumps()?
  3. Decide the result typeAfter loads(), which Python type is available? After dumps(), the result is a string.
  4. Follow data accessWhich dictionary key or list index does the code use after parsing?
  5. Follow changesDoes the code change a Python dictionary or list after parsing it?
  6. Find the final resultDoes the program print a Python value or the JSON text returned by dumps()?

Common beginner mistakes

  • Treating JSON text as if it were already a dictionaryUse json.loads() before trying an expression such as student_json["name"].
  • Expecting loads() to return JSON textloads() returns Python data, such as a dictionary or list, so you can work with it using Python operations.
  • Expecting dumps() to return a dictionary or listdumps() returns one JSON string. Save it under a name such as result when later code needs the text.
  • Forgetting that valid JSON has JSON syntaxJSON text must use its own valid syntax, including the required double quotes around object keys and string values.
  • Losing track of the value typeAt each step, ask whether the name currently holds JSON text, a Python dictionary, a list, or another Python value.
  • Changing the wrong valueChange the parsed Python dictionary or list, then call dumps() when you need fresh JSON text.

Python Playground

Loading Python runtime…

Start with JSON text, use json.loads() to get Python data, change the dictionary, then use json.dumps() to see the new JSON text. Notice which variable holds which type.

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

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.