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 add Key-Value pair to a JavaScript Object?
A JavaScript object has a key-value pair, and it can be of variable length. We first need to declare an object and assign the values to that object for that there can be many methods.Below are the methods to add a key/value pair to a JavaScript object:Table of ContentUsing Dot NotationUsing Bracket
4 min read
How to Perform CRUD Operations on JavaScript Object ?
This article will demonstrate the CRUD Operations on a JavaScript Object. The operations are Create, Read, Update, and Delete. With these operations, we can create, take input, manipulate & destroy the objects. JavaScript Objects are a collection of keys, values or properties/attributes, and ent
4 min read
How To Remove Specific JSON Object From Array JavaScript?
Removing Specific JSON objects from the array is important because it allows for targeted data manipulation and maintenance within JavaScript applications. we will learn to Remove Specific JSON Object From Array JavaScript. These are the following methods: Table of Content Using filter() methodUsing
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 get the size of a JavaScript object ?
In this article, we will see the methods to find the size of a JavaScript object. These are the following ways to solve the problem: Table of Content Using Object.keys() methodUsing Object.objsize() methodUsing Object.entries() methodUsing Object.values() methodUsing Object.keys() methodWe can get t
2 min read
How to Change Specific Object Value to Null in JavaScript ?
We have given an array of objects and our task is to change specific object values to null using JavaScript.Below is an example for a better understanding of the problem statement.Example:Input: { name: 'GeeksforGeeks', topic: 'JavaScript' }Output: { name: 'GeeksforGeeks', topic: null }Table of Cont
3 min read