JavaScript | How to add an element to a JSON object?
In order to add Key/value pair to a JSON object, Either we use dot notation or square bracket notation. Both methods are widely accepted.
Example 1: This example adds {“prop_4” : “val_4”} to the GFG_p object by using dot notation.
<!DOCTYPE html> <html> <head> <title> JavaScript | Add a key/value pair to JSON object </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_up" style=" font-weight: bold"> </p> <button onclick="Geeks()"> Click to add </button> <p id="GFG_down" style="color:green; font-weight: bold" ;> </p> <script> var GFG_p = { prop_1: "val_1", prop_2: "val_2", prop_3: "val_3" }; var p_up = document.getElementById("GFG_up"); var p_down = document.getElementById("GFG_down"); p_up.innerHTML = JSON.stringify(GFG_p); function Geeks() { GFG_p.prop_4 = "val_4"; p_down.innerHTML = JSON.stringify(GFG_p); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:

-
After clicking on the button:

Example 2: This example adds {“prop_4” : “val_4”} to the GFG_p object by using square bracket notation.
<!DOCTYPE html> <html> <head> <title> JavaScript | Add a key/value pair to JSON object </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_up" style=" font-weight: bold"> </p> <button onclick="Geeks()"> Click to add </button> <p id="GFG_down" style="color:green; font-weight: bold" ;> </p> <script> var GFG_p = { prop_1: "val_1", prop_2: "val_2", prop_3: "val_3" }; var p_up = document.getElementById("GFG_up"); var p_down = document.getElementById("GFG_down"); p_up.innerHTML = JSON.stringify(GFG_p); function Geeks() { GFG_p["prop_4"] = "val_4"; p_down.innerHTML = JSON.stringify(GFG_p); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:

-
After clicking on the button:

Recommended Posts:
- Deserializing a JSON into a JavaScript object
- JavaScript | Add new attribute to JSON object
- Converting JSON text to JavaScript Object
- How to send a JSON object to a server using Javascript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- JavaScript | Check if a key exists inside a JSON object
- How to swap key and value of JSON element using JavaScript ?
- JSON | modify an array value of a JSON object
- Convert Java Object to Json String using Jackson API
- Convert Json String to Java Object Using GSON
- Python | Ways to convert string to json object
- Convert Java Object to Json String using GSON
- Javascript | JSON PHP
- JavaScript JSON
- JavaScript | JSON Arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


