Converting JSON text to JavaScript Object
Last Updated :
30 Aug, 2024
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 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 consist of curly braces ( { } ) at the either ends and have key-value pairs inside the braces. Each key-value pair inside braces are separated by comma (, ). JSON object looks something like this :
{
"key":"value",
"key":"value",
"key":"value",
}
Example for a JSON object :
{
"rollno":101",
"name":"Mayank",
"age":20,
}
Conversion of JSON text to Javascript Object
JSON text/object can be converted into Javascript object using the function JSON.parse().
javascript
var object1 = JSON.parse('{"rollno":101, "name":"Mayank", "age":20}');
For getting the value of any key from a Javascript object, we can use the values as: object1.rollno If we pass a invalid JSON text to the function JSON.parse(), it will generate error (no output is displayed when using in tag of HTML). Examples : Here, in example, the JSON text 'jsonobj' have 3 key-value pair. Each of the pairs were able to be accessed by the Javascript object 'obj' using dot ( . ). 'obj' was a javascript object which was the result of the function JSON.parse().
var jsonobj = '{ "name":"Brendan Eich", "designerof":"Javascript", "bornin":"1961" }'; var obj = JSON.parse(jsonobj); print("JSON Object/Text : "); print(obj.name + ", who was born in " + obj.bornin + ", was the designer of " + obj.designerof); print("Use of Javascript object : "); print(jsonobj);
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON to JavaScript Object</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h2 {
color: #333;
}
b {
color: #555;
}
p {
margin: 10px 0;
}
</style>
</head>
<body>
<h2>Converting JSON Text into JavaScript Object</h2>
<b>JSON Object:</b>
<p id="demo"></p>
<b>Use of JavaScript object:</b>
<p id="demo1"></p>
<script>
let jsonobj = '{ "name":"Brendan Eich","designerof":"JavaScript","bornin":"1961" }';
// Here we convert JSON to object
let obj = JSON.parse(jsonobj);
// Display JSON text
document.getElementById("demo").innerHTML = jsonobj;
// Use JavaScript object to display information
document.getElementById("demo1").innerHTML =
obj.name + ", who was born in " +
obj.bornin + ", was the designer of " +
obj.designerof + ".";
</script>
</body>
</html>
Output:
Converting JSON text to JavaScript Object
Similar Reads
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 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 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
JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje
3 min read
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 String to JSON in JavaScript? 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()Us
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
Convert JSON String to Array of JSON Objects in JavaScript Converting a JSON string to an array of JSON objects in JavaScript involves transforming a structured text format (JSON) into a usable JavaScript array. This allows developers to work directly with the data, enabling easier manipulation, analysis, and display of information.1. Using JSON.parse() Met
2 min read
How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 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