Python JSON

Python · Working with Data

Theory

JSON (JavaScript Object Notation) is a text format for structured data, used by almost every web API and many config files. Python's json module converts between JSON text and Python objects. json.dumps(obj) (dump to string) turns a dict or list into JSON text, and json.loads(text) (load from string) parses JSON text back into Python objects. json.dump() and json.load(), without the s, do the same with files.

The mapping is natural: a JSON object becomes a dict, an array a list, a string a str, numbers int or float, true/false become True/False and null becomes None. json.dumps({'name': 'Asha'}) gives the string '{"name": "Asha"}' - note JSON requires double quotes. Use indent=2 for readable output. Some Python types, such as datetime or set, are not JSON-serializable and raise TypeError.

Why we use it

JSON is the common language for exchanging data between programs. The json module lets your Python code read API responses and config files and send data out in a format any system understands.

When to use it

Use json for API payloads, config files and saving simple nested data. For large tables pandas (pd.read_json, CSV, Parquet) is better, and for saving arbitrary Python objects pickle exists, but never unpickle untrusted data.

How it works

  1. Have a Python dict or JSON text
  2. Python -> JSON? Yes: json.dumps(obj) -> JSON string. No: json.loads(text) -> Python object.
  3. Working with a file? Yes: Use json.dump / json.load on it. No: Keep the string in memory.
  4. Data ready to send, store or use

Syntax

import json
json.dumps(obj)
json.loads(text)

Example

data = {'name': 'Asha'}
json.dumps(data)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic