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 );
|
Output
Roll 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
const jsonString =
'{"name": "Nikita", "age": 26, "city": "Noida"}' ;
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
const jsonString =
'{"name": "Rahul", "age": 30, "city": "Noida"}' ;
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 Content Using JSON.stringify with a Replacer Function Using a
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?
JavaScript object has 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 Content Using Dot NotationUsing Bracket No
3 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 Remove Empty Object from JSON in JavaScript ?
In JSON, empty objects can cause data inconsistency and processing issues. We will explore three different approaches filter method, forEach Loop, and for Loop to remove empty objects from JSON in JavaScript. Table of Content Using filter MethodUsing forEach LoopUsing for LoopUsing Array.reduce() Me
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. [GFGTABS] JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 248
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 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
3 min read