Consuming APIs, JSON & Data Retrieval

Understanding and Converting JSON

Learning Outcome

5

Connect JSON to Generative AI responses

4

Apply the right json function to real tasks.

3

Recognize how JSON maps to Python types

2

Identify JSON’s strict syntax rules.

1

Understand what JSON is and why it’s used.

Imagine goods being shipped from Mumbai to different parts of the world.

Standard Shipping Container

The same container works across different ships, cranes, and trucks without repacking.

Packing the Container

Putting goods into the standard container is like serialisation—converting data into JSON.

Unpacking the Container

Taking the goods out at the destination is like deserialisation converting JSON back into usable data.

Now the ship is a Java server and the truck is your Python script. Each stores data differently. How does
the data arrive usable?

Both use the same standard container, JSON. One packs, the other unpacks.

So what does this container look like inside?

What is JSON

JavaScript Object Notation is a lightweight, text-based format that any language can read.

JSON Syntax Rules

Keys use double quotes: "name", never name

Strings use double quotes only; no 'Priya'.

true, false, and null are lowercase.

No trailing commas.

No comments.

Python dicts allow all of these. JSON allows none

Python's JSON Module

 “s” means string. Load = unpack; dump = pack. response.json() = json.loads(response.text).

indent=2, ensure_ascii=False (Hindi text, with utf-8), sort_keys=True

Python's JSON Module

.get("email")

returns None instead of raising a KeyError

02

Type error 

on dump means a set or date; use list(), .isoformat(), or default=str

03

JSON Decoded Error

 means invalid text (single quotes, trailing comma, HTML page), so catch it with try-except.

04

Access like normal Python: data["student"]["courses"][0]

01

JSON Vs Python Dictionary

Real-World Application : Valid or Invalid JSON ?

{'name': 'Priya', 'age': 22} 

 → Invalid: single quotes

{"name": "Rahul", "placed": True}

→ Invalid: capital True

{"course": "Data Science", "batch": null} 

 → Valid

{"city": "Pune", "pincode": 411001,} 

→ Invalid: trailing comma

Why JSON Matters for Generative Ai

Prompts are sent as JSON via requests.post(url, json=payload)

AI responses come back as nested JSON

You can ask a model to answer in JSON, e.g. {"sentiment": "positive"}, then parse it with json.loads().

Models often add extra text or code, so strip them and wrap the parse in try-except

Summary

5

GenAI requests and answers travel as JSON

4

loads/dumps work with strings; load/dump work with files.

3

Objects become dicts; arrays become lists.

2

It uses double quotes, lowercase true/false/null, and no trailing commas.

1

JSON is the standard container for exchanging data.

Quiz

An LLM returns '{"score": 0.92}' as text. Which function converts it?

A.  json.load()

B. json.loads()

C. JSON.dumps()

D.  json.dump()

An LLM returns '{"score": 0.92}' as text. Which function converts it?

A.  json.load()

B. json.loads()

C. JSON.dumps()

D.  json.dump()

Quiz-Answer

Understanding and Converting JSON

By Content ITV

Understanding and Converting JSON

  • 434