How to transform JSON text to a JavaScript object ?
Last Updated :
21 Jul, 2023
JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but it’s available for use by many languages including Python, Ruby, PHP, and Java, and hence, it can be said as language-independent. For humans, it is easy to read and write and for machines, it is easy to parse and generate. It is very useful for storing and exchanging data.
A JSON object is a key-value data format that is typically rendered in curly braces. JSON object consists of curly braces ( { } ) at either end and has key-value pairs inside the braces. Each key-value pair inside braces are separated by a comma (, ). JSON object looks something like this :
{
"key":"value",
"key":"value",
"key":"value",
}
Example for a JSON object :
{
"rollno":101",
"name":"Nikita",
"age":21,
}
There are several methods that can be used to Conversion of JSON text to Javascript Objects.
- Using jSON.parse() method
- Using eval()
- Using Function constructor
Approach 1: Using jSON.parse() method
The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object.
Syntax:
JSON.parse(string, function)
Parameters: This method accepts two parameters as mentioned above and described below
- string: It is a required parameter and it contains a string that is written in JSON format.
- function: It is an optional parameter and is used to transform results. The function called for each item.
Example: In this example, we are using the above-explained approach.
JavaScript
let obj = JSON.parse(
'{"rollno":101, "name": "Nikita","age": 21}');
console.log("Roll no is " + obj.rollno );
console.log("Name is " + obj.name );
console.log("Age is " + obj.age );
OutputRoll no is 101
Name is Nikita
Age is 21
Approach 2: Using eval()
Using eval() to convert a JSON string to a JSON object, the eval() function takes the JSON string as input and treats it as JavaScript code. It executes the code, creating a JavaScript object from the JSON string.
Syntax:
eval(string)
Example: In this example, we are using the above-explained approach.
JavaScript
// JSON text
const jsonString =
'{"name": "Nikita", "age": 26, "city": "Noida"}';
// Transform JSON text to a JavaScript object using eval()
const jsonObj = eval('(' + jsonString + ')');
console.log(jsonObj);
Output{ name: 'Nikita', age: 26, city: 'Noida' }
The Function constructor converts JSON string to a JavaScript object by creating and invoking a function, but it's unsafe and discouraged.
Syntax:
const jsonObj = new Function('return ' + jsonString)();
Example: In this example, we are using the above-explained approach.
JavaScript
// JSON text
const jsonString =
'{"name": "Rahul", "age": 30, "city": "Noida"}';
// Transform JSON text to a
//JavaScript object using Function constructor
const jsonObj = new Function('return ' + jsonString)();
console.log(jsonObj);
Output{ name: 'Rahul', age: 30, city: 'Noida' }
References:
Similar Reads
How to send a JSON object to a server using Javascript? JavaScript Object Notation (JSON). It is a lightweight data transferring format. It is very easy to understand by human as well as machine. It is commonly used to send data from or to server. Nowadays it is widely used in API integration because of its advantages and simplicity.In this example we ar
2 min read
Converting JSON text to JavaScript Object Pre-requisite: JavaScript JSON JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but itâs available for use by many languages including Python, Ruby, PHP, and Java and hence, it can be said as l
3 min read
How to JSON Stringify an Array of Objects in JavaScript ? In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects.Table of ContentUsing JSON.stringify with a Replacer Function Using a C
3 min read
How to Convert JSON Object to CSV in JavaScript ? JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte
3 min read
How to convert an object to string using JavaScript ? To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp
4 min read
How to parse JSON object using JSON.stringify() in JavaScript ? In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax:
2 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
JSON vs JavaScript Object JSON (JavaScript Object Notation) and JavaScript Objects are important for handling data in JavaScript, but they serve different purposes. JSON is a lightweight data format for transferring data, while JavaScript Objects are in-program data structures used for manipulation and logic.What is JSON?JSO
2 min read
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 Convert JS Object to JSON String in JQuery/Javascript? Converting a JavaScript object to a JSON string means using the JSON.stringify() method to transform the object into a JSON-formatted string. This allows for efficient data storage, transmission, and debugging by representing complex data structures in a standardized text format.To Convert JS Object
4 min read