Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Errors, files and libraries · Lesson 20 of 25

    Exchanging data with JSON

    Convert JSON strings to dictionaries and back.

    8 minutes2 games

    JSON is a text format for data exchange, not Python code. json.loads converts a string to Python objects, and json.dumps converts back. JSON true and null become Python True and None.

    Python
    import json
    
    raw = '{"name": "Mona", "scores": [80, 95], "active": true}'
    student = json.loads(raw)
    print(student["name"])
    print(sum(student["scores"]))
    print(json.dumps({"passed": True, "note": None}, sort_keys=True))
    ToolUse

    loads / dumps

    String ↔ object

    load / dump

    File ↔ object

    Important

    Never use eval to read JSON; eval can execute code. loads parses data, but you still need to validate the resulting fields and types.

    Interview question

    Does JSON preserve tuples?

    What's the output?

    import json
    print(json.loads('{"ok": true}')["ok"])

    What will this code print?

    Exercise

    Decode the JSON and print price plus tax.

    Expected output: 114

    Python
    import json
    raw = '{"price": 100, "tax": 14}'
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousReading and writing filesNextClasses and objects
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.