jsguides

JSON.isRawJSON()

JSON.isRawJSON(value)

JSON.isRawJSON() determines whether the passed value is a raw JSON object. Raw JSON objects are special values created by JSON.rawJSON() that represent immutable JSON data. This method provides a way to distinguish raw JSON objects from regular JavaScript objects.

Syntax

JSON.isRawJSON(value)

Parameters

  • value: The value to test.

Return Value

Returns true if value is a raw JSON object created by JSON.rawJSON(). Otherwise, returns false.

Description

A raw JSON object is an exotic object created by JSON.rawJSON() that stores a complete JSON string representation internally. These objects have distinct characteristics that set them apart from regular JavaScript objects:

  • Frozen: Raw JSON objects are immutable and cannot be modified after creation
  • Stringification behavior: When passed to JSON.stringify(), they serialize to just the JSON representation without any object wrapper
  • Internal slot: They have a @@toStringTag slot that returns "JSON Raw JSON"

This method is particularly useful when building custom serialization logic that needs to handle raw JSON objects differently from regular objects. For example, you might want to avoid double-serializing data that already contains raw JSON values, or preserve raw JSON during deep cloning operations.

Examples

Checking for raw JSON objects

const raw = JSON.rawJSON('{"x": 1}');
console.log(JSON.isRawJSON(raw)); // true

const regular = { x: 1 };
console.log(JSON.isRawJSON(regular)); // false

console.log(JSON.isRawJSON("string")); // false
console.log(JSON.isRawJSON(null)); // false
console.log(JSON.isRawJSON([1, 2, 3])); // false

JSON.isRawJSON() returns false for strings, null, arrays, and ordinary objects — it specifically detects the exotic raw JSON wrapper. This narrow check lets you branch your serialization logic: pass raw JSON objects through untouched while fully serializing ordinary objects. Without this guard, you risk calling JSON.stringify() on a value that already contains serialized JSON, producing escaped double quotes or nested string wrappers that corrupt the output.

Using with JSON.stringify

const raw = JSON.rawJSON('{"name": "test"}');
const obj = { data: raw, timestamp: Date.now() };

const json = JSON.stringify(obj);
console.log(json);
// {"data":{"name":"test"},"timestamp":1234567890}

When JSON.stringify() encounters a raw JSON object, it automatically unwraps the internal JSON string, producing valid JSON without requiring any special handling.

How raw JSON fits serialization

Raw JSON values are mainly useful when you already trust a JSON payload and want to move it through your program without rebuilding it as ordinary objects. That can save work in serialization-heavy code paths, especially when one layer receives JSON from a service and another layer simply forwards it. The key point is that JSON.isRawJSON() lets you detect that special wrapper before deciding whether to stringify, clone, or pass the value through unchanged.

Caveats with mixed data

Raw JSON is not a replacement for normal objects. If a value mixes raw JSON with regular properties, you still need to think about how the outer structure should be serialized and validated. In particular, code that expects to inspect nested fields with ordinary property access will not work the same way if those fields are wrapped as raw JSON. Use JSON.isRawJSON() as a guard whenever your code accepts both plain values and raw JSON wrappers from different sources.

Practical use case: avoiding double serialization

function serializeWithRawJSON(obj) {
  // Check if value is already a raw JSON object
  if (JSON.isRawJSON(obj)) {
    return obj; // Return as-is, no need to re-serialize
  }
  return JSON.stringify(obj);
}

const raw = JSON.rawJSON('{"cached": true}');
console.log(serializeWithRawJSON(raw)); // {"cached":true}
console.log(serializeWithRawJSON({ new: "data" })); // {"new":"data"}

See Also

  • JSON.parse() — Parse a JSON string into a JavaScript value
  • JSON.stringify() — Convert a JavaScript value to a JSON string
  • Object — Work with plain objects returned by JSON parsing