How to Check if the Response of a Fetch is a JSON Object in JavaScript? Last Updated : 22 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 is in JSON format or not: Table of Content By using Content-Type HeaderBy Parsing Response BodyBy using Content-Type HeaderThis approach utilizes the Content-Type header of the response to check if it contains JSON data. If the header includes application/json, the response is parsed as JSON. Otherwise, it will be considered non-JSON. We fetch data from the API endpoint using the Fetch API.In the then block, we first check if the response status is OK using response.ok. If not, we throw an error.We then examine the content-type header of the response to determine if it contains JSON data. If it does, we parse the JSON data using response.json().If the response is successfully parsed as JSON, we assign it to the responseData variable and log it to the console.If any errors occur during the fetch operation or parsing, we catch and handle them in the catch block.Example: The below code will explain the use of the Content-Type header to check the response format. JavaScript let responseData; fetch("https://dummyjson.com/products") .then(response => { if (!response.ok) { throw new Error( 'Network response was not ok'); } const contentType = response.headers. get('content-type'); if (contentType && contentType. includes('application/json')) { return response.json(); } else { throw new Error( 'Response is not JSON'); } }) .then(json => { responseData = json; console.log( 'Response is a JSON object:', responseData); }) .catch(error => { console.error( 'There was a problem with the fetch operation:', error); }); Output: By Parsing Response BodyIn this approach, we will attempt to parse the response body directly in JSON using theresponse.json() method. The errors will be handled using the catch callback to identify non-JSON responses. Example: The below code will explain how you can parse fetched response to JSON. JavaScript fetch("https://dummyjson.com/products") .then(response => { if (!response.ok) { throw new Error( 'Network response was not ok'); } return response.json(); }) .then(json => { console.log( 'Response is a JSON object:', json); }) .catch(error => { console.error('Error:', error); }); Output: Comment More infoAdvertise with us Next Article How to Check Object is an Array in JavaScript? L lunatic1 Follow Improve Article Tags : JavaScript Web Technologies JSON Similar Reads How to Check if Object is JSON in JavaScript ? 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 Ch 2 min read How to Check if an Object has a Specific Property in JavaScript ? In JavaScript, objects can have properties that store data or functions. Sometimes it is necessary to check whether an object has a specific property. This can be useful, for example, when you want to avoid calling a function on an object that doesn't have that function defined as a property. In thi 3 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 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 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 if the value is primitive or not in JavaScript ? To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data 3 min read Like