How to Check if Object is JSON in JavaScript ?
Last Updated :
17 Apr, 2024
JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows:
Using Constructor Type Checking
In this approach, we are using Object.prototype.toString.call(obj) to get the constructor type of the object and compare it to [object Object], making sure it's a plain object. We also verify that the object is not an array using Array.isArray(obj), thus determining if it's JSON-like.
Syntax:
Object.prototype.toString.call(obj) === '[object Object]' && !Array.isArray(obj);
Example 1: The below example uses Constructor Type Checking to check if the object is JSON in JavaScript.
JavaScript
const obj = {
name: "GFG",
age: 30,
city: "Noida"
};
const obj2 = new Date();
const isJSON = Object
.prototype
.toString
.call(obj) === '[object Object]'
&&
!Array
.isArray(obj);
const isJSON2 = Object
.prototype
.toString
.call(obj2) === '[object Object]'
&&
!Array
.isArray(obj2);
console.log(isJSON);
console.log(isJSON2);
Example 2: Determining if an object matches JSON structure using a function, then checking user and car objects to validate JSON likeness.
JavaScript
function isJSONObject(obj) {
return obj !== null
&&
typeof obj === 'object'
&&
obj.constructor === Object;
}
const user = {
username: "GFG",
email: "[email protected]",
age: 25
};
const car = {
make: "Toyota",
model: "Camry",
year: 2020
};
const isJSONUser = isJSONObject(user);
const isJSONCar = isJSONObject(car);
console.log(isJSONUser);
console.log(isJSONCar);
Using instanceof
In this approach, we are using the instanceof operator to check if the object is an instance of the Object class and not an array, making sure it's a plain JSON object.
Syntax:
let gfg = objectName instanceof objectType
Example 1: The below example uses instanceof to check if the object is json in JavaScript or not.
JavaScript
const obj = {
name: "GFG",
age: 30,
city: "Noida"
};
const obj2 = ["1", "2", "3"];
const isJSON = obj instanceof Object && !Array
.isArray(obj);
const isJSON2 = obj2 instanceof Object && !Array
.isArray(obj2);
console.log(isJSON);
console.log(isJSON2);
Example 2: Checking if 'data' is a JSON object and 'arrayData' is a JSON array using 'instanceof'. Logging the results for verification.
JavaScript
const data = { id: 1, name: "GFG" };
const arrayData = [1, 2, 3];
const isJSONObject = data instanceof Object
&&
!Array
.isArray(data);
const isArrayJSONObject = arrayData instanceof Object && !Array
.isArray(arrayData);
console.log(isJSONObject);
console.log(isArrayJSONObject);
Similar Reads
How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
How to Check Object is an Array in JavaScript? There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false.
1 min read
How to Check if JSON Key Value is Null in JavaScript ? In JavaScript, the JSON Object can contain null values, which should be identified. The below approaches can be used to check if the JSON key is null. Table of Content Using for Loop and '===' OperatorUsing Object.values() with Array.prototype.includes()Using Array.prototype.some()Using for Loop and
3 min read
How to change JSON String into an Object in JavaScript ? In this article we are going to learn how to change JSON String into an object in javascript, JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet. It appears close to OOP language like JavaScript but cannot be accessed like Jav
3 min read
How to Check if the Response of a Fetch is a JSON Object in JavaScript? In JavaScript, when making HTTP requests using the Fetch API, it's common to expect JSON data as a response from the server. However, before working with the response data, it's essential to verify whether it is indeed a JSON object or not. The below approaches can help you to check if the response
2 min read