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.
// Reading data from your FastAPI backendconst response = await fetch("http://localhost:8000/api/users");const users = await response.json(); // This calls JSON.parse() internallyconsole.log(users); // Array of user objects
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 only supports a subset of JavaScript data types:
Supported
Not supported
Strings, numbers, booleans
undefined
null
Functions
Arrays
Date objects (become strings)
Plain objects
Map, Set
Copy
const data = { name: "Sarah", callback: function() {}, // Dropped date: new Date(), // Becomes a string nothing: undefined, // Dropped};console.log(JSON.stringify(data));// '{"name":"Sarah","date":"2024-01-15T12:00:00.000Z"}'
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).
localStorage only stores strings. If you pass an object, JavaScript calls .toString() on it, which gives you the useless string "[object Object]". Always stringify.