JSON is just a notation; to make the change you want parse it so you can apply the changes to a native JavaScript Object, then stringify back to JSON

var jsonStr = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';

var obj = JSON.parse(jsonStr);
obj['theTeam'].push({"teamId":"4","status":"pending"});
jsonStr = JSON.stringify(obj);
// "{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}"
Answer from Paul S. on Stack Overflow
Top answer
1 of 6
303

JSON is just a notation; to make the change you want parse it so you can apply the changes to a native JavaScript Object, then stringify back to JSON

var jsonStr = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';

var obj = JSON.parse(jsonStr);
obj['theTeam'].push({"teamId":"4","status":"pending"});
jsonStr = JSON.stringify(obj);
// "{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}"
2 of 6
29
var Str_txt = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';

If you want to add at last position then use this:

var parse_obj = JSON.parse(Str_txt);
parse_obj['theTeam'].push({"teamId":"4","status":"pending"});
Str_txt = JSON.stringify(parse_obj);
Output //"{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}"

If you want to add at first position then use the following code:

var parse_obj = JSON.parse(Str_txt);
parse_obj['theTeam'].unshift({"teamId":"4","status":"pending"});
Str_txt = JSON.stringify(parse_obj);
Output //"{"theTeam":[{"teamId":"4","status":"pending"},{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}"

Anyone who wants to add at a certain position of an array try this:

parse_obj['theTeam'].splice(2, 0, {"teamId":"4","status":"pending"});
Output //"{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"4","status":"pending"},{"teamId":"3","status":"member"}]}"

Above code block adds an element after the second element.

🌐
Quora
quora.com › How-do-you-add-elements-to-a-JSON-Array
How to add elements to a JSON Array - Quora
What is the best option to serve an array of JSON objects? ... Add( ) method (JsonArray) Creates one or more new elements and adds them at a specified index or to the end of the JsonArray.
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
JSON Arrays
In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
Jsdiaries
jsdiaries.com › how to add an array element to json object in javascript.
How to add an array element to JSON object in JavaScript. | The JavaScript Diaries
From time to time we need to manipulate JSON data by adding new elements into it. A JSON object is typically more difficult to directly edit as its normally in a string format. So, how can you add an array element into a JSON Object in JavaScript? This is done by using the JavaScript native methods
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-how-to-add-an-element-to-a-json-object
Adding Elements to a JSON Object in JavaScript - GeeksforGeeks
January 17, 2026 - let jsonObject = { key1: 'value1', key2: 'value2' }; jsonObject['newKey'] = 'newValue'; console.log(jsonObject); ... Object.assign() method adds new elements by merging an existing object with another object containing additional properties.
🌐
CodeSpeedy
codespeedy.com › home › how to add elements in json array using javascript
How To Add Elements In JSON Array Using JavaScript - CodeSpeedy
July 17, 2021 - const fs = require("fs"); //The object to be added const addition = { name: "Anjan", description: "This is an example for CodeSpeedy", }; fs.readFile("./code.json", "utf8", function readFileCallback(err, data) { if (err) { console.log(err); } else { var obj = JSON.parse(data); //now converting it to an object obj.notes.push(addition); //adding the data var json = JSON.stringify(obj, null, 2); //converting it back to json fs.writeFile("./code.json", json, "utf8", (err) => { if (err) { console.log(err); } else { console.log("Done"); } }); } });
🌐
Automation Anywhere
community.automationanywhere.com › home › forums › developers forum › how can i add an element to a json array using the json object manager
How can I add an element to a JSON array using the JSON Object Manager | Community
April 4, 2022 - you'd just need to make that a function and pass in your JSON as a string in a list as well as the target object add as a string of the same list. ... Thanks for your reply. I think that your suggestion to use JavaScript is at least a more elegant ...
🌐
SitePoint
sitepoint.com › javascript
Adding item to array in JSON - JavaScript
October 19, 2011 - You can’t assign new values to a JavaScript array by using an index, js handles array indices internally. “Associative” arrays in javascript are not actually arrays, they are JSON. Here is a quick re-write: · Probably best to look up a bit more info on JavaScript arrays and JSON.
Find elsewhere
🌐
Robotastemtraining
robotastemtraining.com › read › how-do-you-add-elements-to-a-json-array
How do you add elements to a JSON Array?
The push() method adds one or more elements to the end of an array and returns the new length of the array. Here’s how you can use it: let fruits = ["Apple", "Banana", "Cherry"]; console.log("Before:", JSON.stringify(fruits)); fruits.push("Date"); console.log("After:", JSON.stringify(fruits));
🌐
Quora
quora.com › I-want-to-add-a-new-JSON-object-to-the-already-existing-JSON-Array-What-are-some-suggestions
I want to add a new JSON object to the already existing JSON Array. What are some suggestions? - Quora
Answer (1 of 5): Simply use push method of Arrays. [code]var data = [ { name: "Pawan" }, { name: "Goku" }, { name: "Naruto" } ]; var obj = { name: "Light" }; data.push(obj); console.log(data); /* 0:{name:"Pawan"} 1:{name: "Goku"} 2:{name: "Naruto"} ...
🌐
how.wtf
how.wtf › add-new-element-to-json-array-with-jq.html
Add new element to JSON array with jq | how.wtf
March 19, 2023 - Adding a new element to an existing JSON array can be completed using jq.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-how-to-add-an-element-to-a-json-object
How to Add an Element to a JSON Object using JavaScript? | GeeksforGeeks
August 27, 2024 - Below are the approaches to conditionally adding a me ... 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 ... In this article, we will swap the key and value of JSON elements using JavaScript.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-add-an-element-to-a-json-object-using-javascript
How to add an element to a JSON object using JavaScript?
February 16, 2023 - var jsonObject = { members: { host: "hostName", viewers: { userName1: "userData1", userName2: "userData2" } } }; console.log("Original JSON object:"); console.log(jsonObject); console.log("\nAdding an element using dot notation:"); jsonObject.members.viewers.userName4 = 'userData4'; console.log("\nJSON object after adding property:"); console.log(jsonObject);
🌐
Redis
redis.io › docs › latest › commands › json.arrappend
JSON.ARRAPPEND | Docs
1 week ago - Add blue to the end of the colors array. JSON.ARRAPPEND returns the new length of the colors array. redis> JSON.ARRAPPEND item:1 $.colors '"blue"' 1) (integer) 3 · Get the updated value of the colors array. redis> JSON.GET item:1 $.colors "[[\"black\",\"silver\",\"blue\"]]" ... With $-based path argument: Array reply of integer replies or null replies, where each element is the array's new length, or null if the matching value is not an array.
🌐
Progress
docs.progress.com › bundle › abl-reference › page › Add-method-JsonArray.html
Add( ) method (JsonArray)
Skip to main contentSkip to search · Powered by Zoomin Software. For more details please contactZoomin