The Wayback Machine - https://web.archive.org/web/20240620172040/https://www.geeksforgeeks.org/javascript-how-to-add-an-element-to-a-json-object/
Open In App

How to add an element to a JSON object using JavaScript ?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript to add an element to a JSON object by simply assigning a value to a new key. We can use the approaches mentioned below.

Let’s first create a JSON object in JavaScript and then implement the possible approaches.

Javascript




let jsonObject = {
    key1: 'value1',
    key2: 'value2'
};


Examples to add an element to a JSON object using JavaScript

1. Using Dot Notation to add an element to a JSON object

Uses the dot notation (object.property) to directly assign a new property to the JSON object.

Example: Below is an Example using dot Notation.

Javascript




let jsonObject = {
    key1: 'value1',
    key2: 'value2'
};
 
jsonObject.newKey = 'newValue';
console.log(jsonObject);


Output

{ key1: 'value1', key2: 'value2', newKey: 'newValue' }

Explanation: This code initializes a JSON object `jsonObject` with two key-value pairs. It then adds a new key `newKey` with the value `’newValue’` using dot notation. Finally, it logs the updated `jsonObject` to the console.

2. Using Bracket notation to add an element to a JSON object

Uses the bracket notation (object['property']) to add a new property to the JSON object.

Example: Below is an Example using Bracket notation.

Javascript




let jsonObject = {
    key1: 'value1',
    key2: 'value2'
};
 
jsonObject['newKey'] = 'newValue';
console.log(jsonObject);


Output

{ key1: 'value1', key2: 'value2', newKey: 'newValue' }

Explanation: This code initializes a JSON object `jsonObject` with two key-value pairs. It then adds a new key `newKey` with the value `’newValue’` using bracket notation. Finally, it logs the updated `jsonObject` to the console.

3. Using Object.assign()

Utilizes the Object.assign() method to merge the original object with a new object containing the additional key-value pair.

Example: Below is an Example using Object.assign().

Javascript




let jsonObject = {
    key1: 'value1',
    key2: 'value2'
};
 
jsonObject = Object.assign({}, jsonObject, { newKey: 'newValue' });
console.log(jsonObject);


Output

{ key1: 'value1', key2: 'value2', newKey: 'newValue' }

Explanation: This code initializes a JSON object `jsonObject` with two key-value pairs. It then creates a new object by merging `jsonObject` with an object containing the additional property `{ newKey: ‘newValue’ }` using `Object.assign()`. Finally, it logs the updated `jsonObject` to the console.

4. Using Spread operator to add an element to a JSON object

Uses the spread operator (...) to create a new object by spreading the properties of the original object and adding the new property.

Example: Below is an Example using Spread operator.

Javascript




let jsonObject = {
    key1: 'value1',
    key2: 'value2'
};
 
jsonObject = { ...jsonObject, newKey: 'newValue' };
console.log(jsonObject);


Output

{ key1: 'value1', key2: 'value2', newKey: 'newValue' }

Explanation: This code initializes a JSON object `jsonObject` with two key-value pairs. It then creates a new object by spreading the properties of `jsonObject` and adding the property `newKey` with the value `’newValue’`. Finally, it logs the updated `jsonObject` to the console.



Similar Reads

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: JSON.stringify(object, replacer, space); Paramete
2 min read
How to transforms JSON object to pretty-printed JSON using Angular Pipe ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will see How to use pipe to transform a JSON object to pretty-printed JSON using Angular Pipe. It meaning that it will take a JSON object string and return it pretty-p
3 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
JavaScript | Add new attribute to JSON object
The task is to add a JSON attribute to the JSON object. To do so, Here are a few of the most used techniques discussed.Example 1: This example adds a prop_11 attribute to the myObj object via var key. C/C++ Code <!DOCTYPE HTML> <html> <head> <title> JavaScript | Add new attribute to JSON object. </title> </head>
2 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How to add nested object to existing JSON using Postman ?
Postman has become a critical tool for developers around the world, particularly when dealing with JSON data. This article will guide you through the process of adding nested objects to an existing JSON using Postman, with a focus on the approaches and syntax involved. Table of Content Understanding JSON and Nested ObjectsThe Role of PostmanAdding
4 min read
How to Remove Element from JSON Object in JavaScript ?
In JavaScript, removing elements from a JSON object is important for modifying data structures dynamically. This object manipulation can help us to create dynamic web pages. The approaches to accomplish this task are listed and discussed below: Table of Content Using delete KeywordUsing filter MethodUsing DestructuringUsing delete KeywordIn this ap
2 min read
How to Initialize a TypeScript Object with a JSON-Object ?
To initialize a TypeScript Object with a JSON-Object, we have multiple approaches. In this article, we are going to learn how to initialize a TypeScript Object with a JSON-Object. Below are the approaches used to initialize a TypeScript Object with a JSON-Object: Table of Content Object.assign Type AssertionSpread OperatorClass InitializationUsing
3 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ?
Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsing reduce()Using a Map for Multiple ValuesUsing fo
3 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read