JSON.isRawJSON()

JSON.isRawJSON(value)
Returns: boolean · Added in ves2024 · Updated March 17, 2026 · JSON
javascript

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

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.

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