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
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.
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.
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.
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.
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 NoneThis 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.
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.
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
- Find the starting valueIs it JSON text in a string, or is it already Python data?
- Find the transformationDoes the code call
json.loads()orjson.dumps()? - Decide the result typeAfter
loads(), which Python type is available? Afterdumps(), the result is a string. - Follow data accessWhich dictionary key or list index does the code use after parsing?
- Follow changesDoes the code change a Python dictionary or list after parsing it?
- 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 asstudent_json["name"]. - Expecting loads() to return JSON text
loads()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 list
dumps()returns one JSON string. Save it under a name such asresultwhen 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.
