{ "id": "0c948f59f519aee46b505c6b", "source": "solid:signals", "type": "github-document", "title": "json", "content": "---\ntitle: json\nuse_cases: \u003e-\n returning api data, action responses, cache control, revalidation strategies,\n server actions\ntags:\n - json\n - api\n - actions\n - queries\n - revalidation\n - response\nversion: \"1.0\"\ndescription: \u003e-\n Return JSON data from actions with query revalidation control. Configure how\n route data updates after mutations for optimal performance.\n---\n\nThe `json` function returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object that contains the provided data.\nIt is intended for sending JSON data from a [query](/solid-router/reference/data-apis/query) or [action](/solid-router/concepts/actions) while also allowing configuration of query revalidation.\n\nThis works both in client and server (e.g., using a server function) environments.\n\n## Import\n\n```ts\nimport { json } from \"@solidjs/router\";\n```\n\n## Type\n\n```ts\nfunction json\u003cT\u003e(\n\tdata: T,\n\tinit: {\n\t\trevalidate?: string | string[];\n\t\theaders?: HeadersInit;\n\t\tstatus?: number;\n\t\tstatusText?: string;\n\t} = {}\n): CustomResponse\u003cT\u003e;\n```\n\n## Parameters\n\n### `data`\n\n- **Type:** `T`\n- **Required:** Yes\n\nThe data to be serialized as JSON in the response body.\nIt must be a value that can be serialized with [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).\n\n### `init`\n\n- **Type:** `{ revalidate?: string | string[]; headers?: HeadersInit; status?: number; statusText?: string; }`\n- **Required:** No\n\nAn optional configuration object with the following properties:\n\n#### `revalidate`\n\n- **Type:** `string | string[]`\n- **Required:** No\n\nA query key or an array of query keys to revalidate.\nPassing an empty array (`[]`) disables query revalidation entirely.\n\n#### `headers`\n\n- **Type:** `HeadersInit`\n- **Required:** No\n\nAn object containing any headers to be sent with the response.\n\n#### `status`\n\n- **Type:** `number`\n- **Required:** No\n\nThe HTTP status code of the response.\nDefaults to [`200 OK`](http://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/200).\n\n#### `statusText`\n\n- **Type:** `string`\n- **Required:** No\n\nThe status text associated with the status code.\n\n## Examples\n\n### Invalidating Data After a Mutation\n\n```tsx\nimport { For } from \"solid-js\";\nimport { query, action, json, createAsync } from \"@solidjs/router\";\n\nconst getCurrentUserQuery = query(async () =\u003e {\n\treturn await fetch(\"/api/me\").then((response) =\u003e response.json());\n}, \"currentUser\");\n\nconst getPostsQuery = query(async () =\u003e {\n\treturn await fetch(\"/api/posts\").then((response) =\u003e response.json());\n}, \"posts\");\n\nconst createPostAction = action(async (formData: FormData) =\u003e {\n\tconst title = formData.get(\"title\")?.toString();\n\tconst newPost = await fetch(\"/api/posts\", {\n\t\tmethod: \"POST\",\n\t\tbody: JSON.stringify({ title }),\n\t}).then((response) =\u003e response.json());\n\n\t// Only revalidate the \"posts\" query.\n\treturn json(newPost, { revalidate: \"posts\" });\n}, \"createPost\");\n\nfunction Posts() {\n\tconst currentUser = createAsync(() =\u003e getCurrentUserQuery());\n\tconst posts = createAsync(() =\u003e getPostsQuery());\n\n\treturn (\n\t\t\u003cdiv\u003e\n\t\t\t\u003cp\u003eWelcome back {currentUser()?.name}\u003c/p\u003e\n\t\t\t\u003cul\u003e\n\t\t\t\t\u003cFor each={posts()}\u003e{(post) =\u003e \u003cli\u003e{post.title}\u003c/li\u003e}\u003c/For\u003e\n\t\t\t\u003c/ul\u003e\n\t\t\t\u003cform action={createPostAction} method=\"post\"\u003e\n\t\t\t\t\u003cinput name=\"title\" /\u003e\n\t\t\t\t\u003cbutton\u003eCreate Post\u003c/button\u003e\n\t\t\t\u003c/form\u003e\n\t\t\u003c/div\u003e\n\t);\n}\n```\n\n## Related\n\n- [`query`](/solid-router/reference/data-apis/query)\n- [`action`](/solid-router/reference/data-apis/action)", "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-router/reference/response-helpers/json.mdx", "metadata": { "path": "src/routes/solid-router/reference/response-helpers/json.mdx", "repo": "solidjs/solid-docs", "repo_url": "https://github.com/solidjs/solid-docs.git", "size": 3487, "source_type": "github" }, "hash": "58797a646e9267305d03a69072200365c78bf909d01341f7edf543ce4dab9444", "timestamp": "2026-02-23T11:43:00.193328749+01:00" }