Every API you will ever call speaks JSON (JavaScript Object Notation) โ a text format for structured data that every language understands.
The syntax (stricter than JS)
{
"name": "Priya",
"cgpa": 8.9,
"subjects": ["DS", "OOP"],
"hostel": null,
"final": true
}- Keys MUST be double-quoted (single quotes = invalid)
- No trailing commas, no comments, no functions or undefined
The two methods
const text = JSON.stringify(obj); // object โ string (send/store) const obj = JSON.parse(text); // string โ object (receive/load) JSON.stringify(obj, null, 2); // pretty-printed
Where it appears
- API responses (
res.json()in fetch) - localStorage (strings only โ stringify first)
- package.json, config files everywhere
Debug tip: "Unexpected token in JSON at position 0" almost always means your API returned an HTML error page, not JSON โ log the raw response text.