How to Create and Manipulatinag JSON Data in javaScript?
Last Updated :
14 May, 2024
The JavaScript Object Notation (JSON) is a lightweight data interchange format commonly used for transmitting data between the server and a web application. The JSON data is represented as the key-value pairs and is easy to read and write for both humans and machines. In JavaScript, JSON objects can be easily created and manipulated using built-in methods and functions.
Creating JSON Data
JSON data consists of key-value pairs enclosed in curly braces{}
, with keys and values separated by a colon ":"
. Arrays are represented using square brackets []
. To create JSON data in JavaScript we can define an object literal with the key-value pairs and then use the JSON.stringify( ) method to convert the object into the JSON string.
Syntax:
// Define an object literal
const jsonObject = {
key1: value1,
key2: value2,
// Add more key-value pairs as needed
};
// Convert object to JSON string
const jsonString = JSON.stringify(jsonObject);
Example: Implementation to create a JSON data using javascript method.
JavaScript
const person = {
name: "Kumar",
age: 30,
city: "Bangalore"
};
// Convert object to the JSON string
const jsonPerson = JSON.stringify(person);
console.log(jsonPerson);
Output{"name":"Kumar","age":30,"city":"Bangalore"}
Parsing JSON Data
To parse a JSON string back into a JavaScript object, you can use the JSON.parse()
method.
Example: Implementation of JSON.parse() method.
JavaScript
const jsonString = {
"name": "Kumar",
"age": 30,
"city": "Bangalore"
};
const jsonObject = jsonString;
console.log(jsonObject);
Output{ name: 'Kumar', age: 30, city: 'Bangalore' }
Manipulating JSON Data
To manipulate JSON data in JavaScript we can parse a JSON string into the JavaScript object using JSON.parse() method. Once parsed, we can access and modify the properties of the JavaScript object as needed.
Syntax:
// Parse JSON string into the JavaScript object
const jsonObject = JSON.parse(jsonString);
To update an existing property, simply assign a new value to it:
jsonString.name='GeeksforGeeks'
If you need to remove a property, you can use the delete
keyword:
delete jsonString.age;
Example: Implementation to showcase the manipulation of the JSON data.
JavaScript
const jsonCar = '{"make":"Toyota","model": "Camry", "year": 2024}';
const car = JSON.parse(jsonCar);
// Access and modify properties
// of JavaScript object
console.log(car.make);
console.log(car.model);
// Modify the year property
car.year = 2020;
// Convert JavaScript object
// back to the JSON string
const updatedJsonCar = JSON.stringify(car);
console.log(updatedJsonCar);
OutputToyota
Camry
{"make":"Toyota","model":"Camry","year":2020}
Working with Nested Data
JSON supports nested objects and arrays, allowing you to represent complex data structures. For example:
const employee = {
"name": "Rajveer",
"department": {
"name": "Engineering",
"location": "India"
},
"projects": [
{ "name": "Project A", "status": "In Progress" },
{ "name": "Project B", "status": "Completed" }
]
};
We can access nested properties using dot notation or bracket notation:
console.log(employee.department.name); // Output: Engineering
console.log(employee["projects"][0].name); // Output: Project A
Conclusion
JSON is a lightweight data interchange format that is widely used in web development. In JavaScript, you can easily create, parse, and manipulate JSON data using built-in methods like JSON.stringify()
and JSON.parse()
. Understanding how to work with JSON data is essential for handling data from APIs, storing configurations, and communicating between client and server applications.
Similar Reads
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
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 Blob Data to JSON in JavaScript ?
When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use t
2 min read
How to Parse JSON Data in JavaScript?
To parse JSON data in JavaScript, you can use the JSON.parse() method. This method converts a JSON string into a JavaScript object, making it easier to work with the data.1. Parse Simple JSON StringsJavaScriptconst jsonS = '{"name": "Rahul", "age": 25, "city": "Mumbai"}'; const obj = JSON.parse(json
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 Add Backslash in JSON String JavaScript ?
In JavaScript, adding a backslash to a JSON string is important to properly escape special characters, ensuring the integrity and correctness of the JSON format for data processing and storage. Table of Content Using JSON.parse() and JSON.stringify()Using for LoopUsing Array.prototype.map() and Stri
2 min read
How to Create Dynamic Values and Objects in JavaScript?
In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values and object properties at runtime for flexibility and
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 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