JSON is a structured format used to move and store data in API responses, document-oriented workflows, and nested records. Its consistent shape lets Python code extract fields, transform records, and load results into pandas DataFrames. section 4.1.1 section 4.3.3
For API work, request JSON explicitly when available, then parse the response and select needed fields. A number-trivia response contains a fact, its number, a found flag, and a type; the pipeline keeps the useful values while logging failed requests and status errors. section 4.1.1
API response to DataFrame
A request such as `http://numbersapi.com/42?json` returns structured data. Python can call `response.json()`, read the `text` field with a fallback value, append `{Number, Fact}` records, and create a DataFrame for analysis. Add timeouts, retries, status checks, and logging rather than assuming every response is usable. section 4.1.1
JSON is also useful when tabular output is not the destination. Regex-extracted phone components can become a list of JSON objects with `area_code`, `first_3`, and `last_4` keys, ready for MongoDB, REST APIs, or other key-value document workflows. A DataFrame can be converted later with `df.to_json()`. section 4.3.3
Nested JSON preserves relationships but is less convenient for analysis. Flatten it by traversing nested objects and arrays, carrying shared parent values into each record, combining nested author fields, joining genre arrays, and returning a DataFrame. This approach requires knowing the expected structure and handling its edge cases. section 7.2
Listing 7.3 AI - Complex Text Processing with Regular Expressions
json_data = {
"library": {
"name": "City Library",
"location": "Downtown",
"books": [
{
"title": "Python Programming",
"author": {
"first_name": "John",
"last_name": "Doe"
},
"genres": ["Programming", "Technology"],
"published_year": 2020
},
{
"title": "Data Science 101",
"author": {
"first_name": "Jane",
"last_name": "Smith"
},
"genres": ["Data Science", "AI"],
"published_year": 2019
}
]
}
}
Inspect a nested library JSON object containing shared library fields, book records, nested author objects, and genre arrays before flattening it into rows. section 7.2
JSON can also expose data that is awkward to scrape from HTML. For product variants, request a product page’s `.js` endpoint, parse the returned JSON, find the variant matching a target label, and return its price; return `None` when no match exists. section 10.8









































