How to Check if Object is JSON in JavaScript ? Last Updated : 17 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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: Table of Content Using Constructor Type CheckingUsing instanceofUsing Constructor Type CheckingIn 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); Outputtrue false 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: "gfg@gmail.com", age: 25 }; const car = { make: "Toyota", model: "Camry", year: 2020 }; const isJSONUser = isJSONObject(user); const isJSONCar = isJSONObject(car); console.log(isJSONUser); console.log(isJSONCar); Outputtrue true Using instanceofIn 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 objectTypeExample 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); Outputtrue false 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); Outputtrue false Comment More infoAdvertise with us Next Article How to Check if Object is JSON in JavaScript ? A anjalibo6rb0 Follow Improve Article Tags : JavaScript Web Technologies JSON 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 Iterate JSON Object in JavaScript? In JavaScript, there are different ways to iterate over the properties of a JSON object. Letâs look at the most common techniques.1. Using for...in LoopThe for...in loop is a simple way to go through the properties of a JSON object. It loops over the keys of the object. Inside the loop, we can acces 4 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 How to check if an array includes an object in JavaScript ? Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array.How to check if an array inclu 3 min read How to Check if a Value is a Number in JavaScript ? To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number.Methods to Check if a Value is a NumberThere are various ways to check if a value 3 min read How to Check a Key Exists in JavaScript Object? Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in 2 min read How to check a JavaScript Object is a DOM Object ? Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces.Wha 2 min read Like