Understanding JSON and APIs
What is JSON?
JSON (JavaScript Object Notation) is a lightweight text format for representing structured data. It’s become the standard way applications exchange data over the web.
{ "id": 101, "title": "Learn Python", "tags": ["python", "beginner"], "published": true, "author": { "name": "Asha", "id": 7 } } JSON Rules
- Keys are always strings, wrapped in double quotes
- Values can be strings, numbers, booleans, null, arrays, or nested objects
- No trailing commas, no comments — unlike JavaScript object literals
What is an API?
An API (Application Programming Interface) is a defined way for one program to request data or actions from another. A REST API exposes data over HTTP using URLs and standard verbs.
| Method | Meaning |
|---|---|
| GET | Retrieve data |
| POST | Create new data |
| PUT/PATCH | Update existing data |
| DELETE | Remove data |
Calling an API from JavaScript
const response = await fetch("https://api.example.com/tutorials/101"); const data = await response.json(); console.log(data.title); Parsing and Creating JSON in Code
const obj = JSON.parse('{"name": "Asha"}'); // JSON text → object const text = JSON.stringify({ name: "Asha" }); // object → JSON text Practice Exercise
Write a JSON object representing a tutorial (title, author, tags array, published boolean), then write JavaScript to fetch it from a mock API and log just the tags.
Leave a Reply