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 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
Where you’ll use it
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
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:Common mistakes
Parsing a value that's already an object
Parsing a value that's already an object
Forgetting to stringify when saving to localStorage
Forgetting to stringify when saving to localStorage
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