How to Convert String to JSON in JavaScript?
Last Updated :
19 Apr, 2024
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:
Using 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 Constructor
In 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' }
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