main
Branches
main (2.15.1)dev
Versions
2.15.11.19.3v0.21.0
React Router v7 has been released. View the docs
json

json

This is a shortcut for creating application/json responses. It assumes you are using utf-8 encoding.

import { json } from "@remix-run/node"; // or cloudflare/deno

export const loader = async () => {
  // So you can write this:
  return json({ any: "thing" });

  // Instead of this:
  return new Response(JSON.stringify({ any: "thing" }), {
    headers: {
      "Content-Type": "application/json; charset=utf-8",
    },
  });
};

You can also pass a status code and headers:

export const loader = async () => {
  return json(
    { not: "coffee" },
    {
      status: 418,
      headers: {
        "Cache-Control": "no-store",
      },
    }
  );
};
Docs and examples licensed under MIT