JSON.isRawJSON()
The JSON.isRawJSON()
static method in JavaScript checks whether a given value is a Raw JSON object
. This method is part of the JavaScript JSON API and is used to validate if a value represents raw JSON data that hasn’t been parsed or modified.
This method is not available in Chrome and Edge, but not in Firefox, Safari, etc.
Syntax
</>
Copy
JSON.isRawJSON(value)
Parameters
Parameter | Description |
---|---|
value | The value to check if it is a raw JSON object. |
Return Value
The JSON.isRawJSON()
method returns a boolean:
true
: If the provided value is a raw JSON object.false
: Otherwise.
Examples
1. Checking if a Value is Raw JSON
This example demonstrates how JSON.isRawJSON()
verifies if a value is raw JSON.
</>
Copy
const rawJSON = JSON.rawJSON({ key: "value" });
const notRawJSON = { key: "value" };
console.log(JSON.isRawJSON(rawJSON)); // true
console.log(JSON.isRawJSON(notRawJSON)); // false
Output
true
false
JSON.isRawJSON(rawJSON)
returnstrue
because the value is created as raw JSON.JSON.isRawJSON(notRawJSON)
returnsfalse
because it is a standard JavaScript object, not raw JSON.
2. Using JSON.isRawJSON()
with Nested Objects
Even with nested structures, JSON.isRawJSON()
validates only the top-level value as raw JSON.
</>
Copy
const rawJSONNested = JSON.rawJSON({ nested: { key: "value" } });
const notRawJSONNested = { nested: JSON.rawJSON({ key: "value" }) };
console.log(JSON.isRawJSON(rawJSONNested)); // true
console.log(JSON.isRawJSON(notRawJSONNested)); // false
Output
true
false
3. Invalid Inputs for JSON.isRawJSON()
When an invalid or unrelated value is passed, JSON.isRawJSON()
always returns false
.
</>
Copy
console.log(JSON.isRawJSON("string")); // false
console.log(JSON.isRawJSON(42)); // false
console.log(JSON.isRawJSON(null)); // false
Output
false
false
false
The method JSON.isRawJSON()
is strict about the type of value it evaluates.