Skip to content

Working with JSON and APIs

Programming Node.js Difficulty 1/5

Beginning of the lesson

Working with JSON and APIs

How Node.js programs send and receive data over the internet

Big Idea: Programs need a shared language to exchange data. JSON is that language — a simple text format that both your Node.js server and the whole internet understand. An API is a doorway that lets one program ask another for data, and JSON is what travels through the door.
By the end you will be able to:
  • turn a JavaScript value into JSON text with JSON.stringify, and back with JSON.parse,
  • explain what a REST API is in plain words,
  • send a JSON response from Express with res.json(),
  • call another API from Node with fetch and read its JSON.

1. What is JSON?

JSON stands for JavaScript Object Notation. It is just text that describes data using curly braces, square brackets, numbers, and strings in double quotes. Because it is plain text, it can be saved to a file or sent across the internet, and almost every programming language can read it.

A JavaScript object lives inside your program's memory. JSON is the text version of that data. Two built-in tools move between them:

  • JSON.stringify(value) - turns a JS value into a JSON string (to send or save it).
  • JSON.parse(text) - turns a JSON string back into a JS value (to use it).
const user = { name: "Ada", age: 36, likes: ["math", "code"] };

const json = JSON.stringify(user);
console.log(json);
console.log(typeof json);

// Output:
// {"name":"Ada","age":36,"likes":["math","code"]}
// string

Notice the result is a string. Every key and every text value is wrapped in double quotes — that is a strict rule of JSON. To go the other way, use JSON.parse:

const text = '{"name":"Ada","age":36}';

const obj = JSON.parse(text);
console.log(obj.name);
console.log(obj.age + 1);

// Output:
// Ada
// 37
Remember: after parse, obj.age is a real number, so obj.age + 1 gives 37, not "361".

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers