Serving JSON: How to Send Data From Your Server
Until now, your server has been sending back web pages, the kind a person opens and reads. But the biggest sites on the internet do something different. They send raw data instead. In this lesson we cover serving JSON, where your own server stops being the customer fetching data and becomes the kitchen handing it out.
What does serving JSON mean?
Serving JSON means your server sends back raw data instead of a web page, in a clean format called JSON that any app can read. A page is built for humans to look at. JSON is built for programs to use. When you serve JSON, you become the source of data, and other apps can ask your server for it whenever they need it.
What is an API and where does JSON fit in?
An API is data on demand. It’s a way for one program to ask another for information and get a useful answer back. When your server hands out raw data like this, you’ve built an API.
The format that data usually travels in is JSON. JSON is just a simple, tidy way of writing data so that programs can read it easily. Think of it as data with clear labels and values, neatly arranged.
If APIs and JSON still feel new, it helps to walk through the foundations first. You can do that step by step in the free Zero to AI Hero course, with a fresh 2-minute lesson every day.
Why flip from fetching data to serving it?
Earlier in this journey, your app was the customer. It fetched data from other people’s servers, like ordering food from a kitchen.
Today you flip that around. Your own server becomes the kitchen. Instead of asking others for data, you hand out your own. Any app, anywhere, can now read what you serve.
That’s a big moment. You’re no longer just using other people’s data. You are the source.
How do I actually serve JSON in code?
The good news: it takes one line. Here it is:
res.json({ chai: 10 })
Let’s read that slowly, piece by piece:
res.jsonmeans “send this back as data, not as a page.”- The curly braces
{ }hold the payload, which is just a fancy word for the actual data you’re sending. - Inside,
chai: 10is one piece of data: a label (chai) and its value (10).
So this single line tells your server: when someone asks, hand back this clean data. That’s serving JSON in action.
What’s the difference between a page and JSON?
This is the heart of the whole idea, so it’s worth saying plainly:
- A page is for humans. It has colours, buttons, and text you read with your eyes.
- JSON is for programs. It has no styling, just clean data that other code can pick up and use.
Both come from your server. The difference is simply who they’re meant for. When you serve JSON, you’re talking to other programs, not people.
How do I test that my server is serving JSON?
You can try this right now, and it takes less than a minute. First, run your server. Then, in your terminal, type this command:
curl localhost/menu
The curl command fetches whatever your server sends back, straight into your terminal. If everything is wired up, clean JSON comes back, something like this:
{ "chai": 10 }
That’s your own API answering. You asked your server for data, and it served you clean JSON. No web page, just data on demand.
If you’d like a refresher on running your server and what localhost means before this, those steps come just before this lesson in the same level. They make this part click much faster.
Why does serving JSON matter for what comes next?
Because now you are the source of data. Any app can read what your server hands out. That’s the same way real companies expose their data, from weather apps to payment systems.
This skill also sets you up beautifully for building with AI. When you later use tools like Claude Code to build real apps, you’ll create and serve data exactly like this. Understanding it now means you’ll know what your AI helper is actually doing, instead of just hoping it works.
Key takeaways
- Serving JSON means your server sends raw data instead of a web page.
- An API is data on demand, and JSON is the clean format that data travels in.
- You flip from being the customer (fetching data) to the kitchen (serving your own).
- One line does it:
res.json({ chai: 10 }), whereres.jsonsends data and the curly braces hold the payload. - A page is for humans; JSON is for programs.
- Test it with
curl localhost/menuand watch clean JSON come back.
That’s serving JSON, fully explained. Next up, we’ll do the opposite and look at serving HTML, sending back a real page for humans to read. You can follow the whole journey and learn step by step with a new 2-minute lesson every day.
🚀 Take the full free course: Zero to AI Hero — learn to build with AI from scratch. New lessons daily, in Hindi & English.
Want to actually learn this?
151 free 2-minute lessons — from "what's a file?" to building with AI.
▶ Start the free course