Skip to main content

What is JSON?

JSON (JavaScript Object Notation) is a text format for exchanging data between systems. When your React frontend talks to your FastAPI backend, the data travels as JSON strings over HTTP.
JSON looks like a JavaScript object, but it’s a string. Every key must be in double quotes. No trailing commas, no comments, no functions — just data.
JSON is language-independent. Python uses json.dumps() and json.loads(). JavaScript uses JSON.stringify() and JSON.parse(). FastAPI automatically converts Pydantic models to JSON responses.

JSON.stringify() — object to string

Convert a JavaScript object into a JSON string. You do this when sending data to an API.

Pretty printing

The third argument controls indentation. Useful for debugging and logging.

Where you’ll use it

The body of a fetch request must be a string. JSON.stringify() converts your object to the JSON string the server expects.

JSON.parse() — string to object

Convert a JSON string back into a JavaScript object. You do this when receiving data from an API.

Where you’ll use it

You rarely call JSON.parse() directly. The response.json() method from fetch does it for you. But you’ll use JSON.parse() when reading from localStorage or processing raw JSON strings.

JSON and localStorage

localStorage can only store strings. Use JSON.stringify() and JSON.parse() to save and load objects:

What JSON can’t store

JSON only supports a subset of JavaScript data types:
undefined and functions are silently dropped during JSON.stringify(). Dates become ISO strings. If you JSON.parse() a date string, you get a string back — not a Date object. Convert it manually with new Date(dateString).

Common mistakes

response.json() calls JSON.parse() internally. Don’t double-parse. If you have an object, you don’t need JSON.parse(). If you have a string, you do.
localStorage only stores strings. If you pass an object, JavaScript calls .toString() on it, which gives you the useless string "[object Object]". Always stringify.

What’s next?

You can convert data between JavaScript objects and JSON strings. Now let’s learn how to make decisions in your code with conditionals.

Conditionals

if/else, ternary operators, and logical operators