0Pricing
Sveltejs Academy · Lesson

Returning JSON Responses

Use the json() helper to return structured JSON with the correct content-type.

Returning JSON Responses is a free Sveltejs Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The json Helper

Use json() from @sveltejs/kit to return JSON with proper headers.

import { json } from "@sveltejs/kit";
return json({ ok: true });

Status Codes

Pass a second options object to control status and additional headers.

return json({ ok: false }, { status: 400 });

Custom Headers

Add cache-control, CORS, or anything else via headers.

return json(data, { headers: { "cache-control": "max-age=60" } });

Manual Response

Or return a raw Response object if you need a non-JSON body.

return new Response("plain text", { headers: { "content-type": "text/plain" } });

Type-Safe Body

With TypeScript you can type the response body for consumer convenience.

Streaming JSON

For huge collections, stream chunks of newline-delimited JSON.

Pagination

Include nextCursor or page metadata in your responses for clean pagination.

Error Shape

Adopt a consistent error envelope like { error: { code, message } } for clarity.

Etag Caching

Set ETag headers on stable responses for HTTP-level caching.

Content Negotiation

Use Accept header to switch between JSON and other formats.

Best Practice

Always return JSON for APIs, even on errors. Clients expect consistent shape.

Quick Check

How do you return JSON with proper headers?

Recap

Use json() for ergonomic JSON responses with status and headers. Return raw Response for non-JSON.

Frequently asked questions

Is the “Returning JSON Responses” lesson free?

Yes — the full text of “Returning JSON Responses” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “Returning JSON Responses”?

Use the json() helper to return structured JSON with the correct content-type. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Returning JSON Responses” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. +server.js: GET POST PUT DELETE
  2. Request and RequestEvent
  3. Returning JSON Responses
  4. Streaming with ReadableStream
← Back to Sveltejs Academy