How to Convert String to JSON in JavaScript? Last Updated : 19 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, converting a string to JSON is important for handling data interchangeably between server and client, parsing external API responses, and storing structured data in applications. Below are the approaches to converting string to JSON in JavaScript: Table of Content Using JSON.parse()Using eval()Using Function ConstructorUsing JSON.parse()In this approach, we are using JSON.parse() in JavaScript to convert a JSON-formatted string (str) into a JavaScript object (res). This method parses the string according to JSON syntax. Syntax:JSON.parse( string, function(optional) )Example: The below code will explain the use of the JSON.parse() method to convert a string in JSON. JavaScript let str = `{ "name": "GeeksforGeeks", "CEO": "Sandeep Jain" }`; let res = JSON.parse(str); console.log(res); Output{ name: 'GeeksforGeeks', CEO: 'Sandeep Jain' } Using eval()In this approach, we are using eval() in JavaScript to directly evaluate and execute the JSON-formatted string (str) as JavaScript code, resulting in a JavaScript object (res) representing the parsed JSON data. Syntax:eval(str)Example: The below code provides the implementation to convert string to JSON using eval(). JavaScript let str = `{ "name": "GeeksforGeeks", "CEO": "Sandeep Jain" }`; let res = eval('(' + str + ')'); console.log(res); Output{ name: 'GeeksforGeeks', CEO: 'Sandeep Jain' } Using Function ConstructorIn this approach, we are using the Function Constructor in JavaScript to create a new function that returns the evaluated JSON-formatted string (str) as a JavaScript object (res). Example: The below code example implements the Function() constructor which returns the Javascript Object. JavaScript let str = `{ "name": "GeeksforGeeks", "CEO": "Sandeep Jain" }`; let res = new Function('return ' + str)(); console.log(res); Output{ name: 'GeeksforGeeks', CEO: 'Sandeep Jain' } Comment More infoAdvertise with us Next Article How to Convert String to JSON in JavaScript? G gpancomputer Follow Improve Article Tags : JavaScript Web Technologies JSON Similar Reads How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used 3 min read How to Convert a Map to JSON String in JavaScript ? A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A 2 min read How To Convert Base64 to JSON String in JavaScript? There could be situations in web applications, where there is a need to decode the data from Base64 format back into its original JSON format. It generally happens when one has to transmit data over the network where Base64 encoding is well suited for encoding binary data.In this article, we will se 2 min read How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert 3 min read How to Create JSON String in JavaScript? JSON strings are widely used for data interchange between a server and a client, or between different parts of a software system. So converting objects to JSON strings is very important for good client-server communication. Below are the following approaches to creating a JSON string: Table of Conte 2 min read How to Convert XML to JSON in JavaScript? To convert XML to JSON in JavaScript, various methods and libraries and be used. Here, we use xml-js library that provides xml2json function to convert XML to JSON data. It takes XML data as input and gives the JSON objects as output. We can also use the DOMParser from the xmldom package to convert 2 min read How to Convert CSV to JSON in JavaScript ? In this article, we will explain different ways to change Comma-Separated Values (CSV) data into JavaScript Object Notation (JSON) format, step-by-step. We'll break down each method with clear explanations and examples. There are several approaches available in JavaScript to convert CSV to JSON in J 3 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read How to Convert HTML to JSON in JavaScript ? Converting HTML to JSON is important for structured data extraction and integration with JavaScript applications. Here, we will learn different approaches to converting HTML to JSON in JavaScript. Below are the approaches to convert html to JSON in JavaScript: Table of Content Using html-to-json Lib 2 min read How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light 2 min read Like