JavaScript JSON.rawJSON() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
JSON

Definition

In JavaScript, JSON.rawJSON() takes a string that is already valid JSON and wraps it as a special β€œraw” value. When you later call JSON.stringify(), that raw value is pasted into the output as-is.

This avoids double-encoding. A normal string becomes "..." with quotes and escapes. A raw JSON value is treated as ready-made JSON text: a number, object, array, and so on. Check values with JSON.isRawJSON().

Syntax

Pass a complete JSON text string. Then put the result where you would put any other value before stringifying.

JavaScript
JSON.rawJSON(text);

const payload = {
  count: JSON.rawJSON("42")
};
JSON.stringify(payload);
CallWhat it does
JSON.rawJSON("42")Wraps the number text 42 so stringify writes a bare number.
JSON.rawJSON('{"ok":true}')Wraps a small object so it is nested without being turned into a quoted string.

The argument must be a full JSON value. Partial fragments like "a":1 without braces are invalid.

Library declarations
JavaScript
// ECMAScript 2024 (ES2024)
JSON.rawJSON(text)

// text    : a string that is valid JSON text
// returns : a raw JSON value for use with JSON.stringify()
// throws  : SyntaxError when text is not valid JSON

Parameters

ParameterDescription
textRequired. A string of valid JSON: a number, string, boolean, null, object, or array.

Return Value

Return ValueDescription
Raw JSON valueA special object that JSON.stringify() embeds as the original text.
SyntaxError (thrown)Thrown when text is not valid JSON.

JavaScript Examples

Example 1 — Embed a Number as Raw JSON

Write a large number as bare JSON text instead of a quoted string.

JavaScript
if (typeof JSON.rawJSON !== "function") {
  console.log("JSON.rawJSON is not supported here.");
} else {
  const data = {
    id: JSON.rawJSON("9007199254740993")
  };
  console.log(JSON.stringify(data));
}
Try It Yourself

How It Works

  1. The string "9007199254740993" is valid JSON for a number.
  2. JSON.rawJSON wraps that text.
  3. JSON.stringify pastes it as a bare number in the result.

Example 2 — Nest an Already-Made Fragment

You already have a JSON object as text. Embed it inside a larger payload.

JavaScript
if (typeof JSON.rawJSON !== "function") {
  console.log("JSON.rawJSON is not supported here.");
} else {
  const fragment = '{"city":"Paris","ok":true}';
  const payload = {
    meta: { source: "api" },
    body: JSON.rawJSON(fragment)
  };
  console.log(JSON.stringify(payload));
}
Try It Yourself

How It Works

  1. fragment is already a JSON object string.
  2. Wrapping it with rawJSON marks it as ready text.
  3. Stringify nests the object, not a quoted copy of the string.

Example 3 — Raw JSON vs a Normal String

See the difference between a plain string and a raw JSON value.

JavaScript
if (typeof JSON.rawJSON !== "function") {
  console.log("JSON.rawJSON is not supported here.");
} else {
  const asString = { n: "42" };
  const asRaw = { n: JSON.rawJSON("42") };

  console.log("String: " + JSON.stringify(asString));
  console.log("Raw:    " + JSON.stringify(asRaw));
  console.log("isRawJSON: " + JSON.isRawJSON(asRaw.n));
}
Try It Yourself

How It Works

  1. A normal string stays quoted in the JSON output.
  2. A raw value is written as the number 42.
  3. JSON.isRawJSON confirms the wrapped value.

Browser & Runtime Support

JSON.rawJSON() is part of ES2024. Support is growing. Check before you use it in production, or feature-detect with typeof JSON.rawJSON === "function".

✓ ES2024 · Newer API

JSON.rawJSON()

Available in recent Chrome, Edge, Firefox, Safari, and Node.js versions that implement JSON raw values. Older engines do not have it.

ES2024 Standard since
Google Chrome 114+ · Desktop & Mobile
Full support
Microsoft Edge 114+ · Chromium
Full support
Mozilla Firefox 126+ · Desktop & Mobile
Full support
Apple Safari 17.4+ · macOS & iOS
Full support
Opera 100+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Varies
Android WebView 114+ · Chromium based
Supported
Node.js 21+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
JSON.rawJSON() Growing

Bottom line: Feature-detect before use. For everyday parse and stringify needs, JSON.parse() and JSON.stringify() are enough.

Frequently Asked Questions

It takes a string that is already valid JSON and wraps it as a special raw value. When JSON.stringify() sees that value, it pastes the text as-is instead of turning it into a quoted string again.
Use it when you already have a JSON fragment as text and you want to nest it inside a larger object without double-encoding. It is also useful for values that stringify cannot write by itself, such as a BigInt written as a number.
A raw JSON object. You do not read it like a normal object. Pass it to JSON.stringify(), or check it with JSON.isRawJSON().
JSON.rawJSON() throws a SyntaxError. The text must be a complete JSON value: a number, string, boolean, null, object, or array.
No. It is a newer method (ES2024). Check support in your browser or Node.js version, or use JSON.isRawJSON to detect the feature at runtime.
A normal string becomes a quoted JSON string with escapes. A raw JSON value is inserted as raw text, so 9007199254740993n can appear as a bare number in the output.

Next: JSON.isRawJSON()

Check whether a value came from JSON.rawJSON().

JSON.isRawJSON() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P πŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful