JSON stands for JavaScript Object Notation. A JSON object is really a string that has yet to be turned into the object it represents.

To add a property to an existing object in JS you could do the following.

object["property"] = value;

or

object.property = value;

If you provide some extra info like exactly what you need to do in context you might get a more tailored answer.

Answer from Quintin Robinson on Stack Overflow
Discussions

Insert one json object into another?
I would like to insert one json object into another. Does Nlohmann have functionality built-in to handle this easily or do we have to manually iterate and merge? For example, here are the two json ... More on github.com
🌐 github.com
1
1
January 28, 2023
Adding items to a JSON object - JavaScript
hi, Does anyone know the function for adding more items to an already declared JSON object? Assuming you have something like: var event= [{}]; // this is the correct empty JSON declaration how can i add an itemt to the object? I used push but not the right function. Thanks More on sitepoint.com
🌐 sitepoint.com
0
March 3, 2009
Add an object to existing json
I want to add an object(key value pair) to a payload that i am getting from the external data file. “payload”:"{“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}" What I want to do is add (“key4”:“value4”) to the payload that I use in request body. More on community.postman.com
🌐 community.postman.com
1
0
January 29, 2021
python - How to append data to a json file? - Stack Overflow
Specifically, json objects have a syntax that means the whole object must be read and parsed in order to understand any part of it. Fortunately, there are lots of other options. A particularly simple one is CSV; which is supported well by python's standard library. The biggest downside is that it only works well for text; it requires additional ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-a-json-string-to-an-existing-json-file-in-java
How to add a JSON string to an existing JSON file in Java?
import java.io.*; import java.util.*; import com.google.gson.*; import com.google.gson.stream.*; import com.google.gson.annotations.*; public class JSONFilewriteTest { public static String nameRead; public static void main(String[] args) { try { JsonParser parser = new JsonParser(); Object obj = parser.parse(new FileReader("employee1.json")); JsonObject jsonObject = (JsonObject) obj; System.out.println("The values of employee1.json file:\n" + jsonObject); JsonArray msg = (JsonArray)jsonObject.get("emps"); Iterator<JsonElement> iterator = msg.iterator(); while(iterator.hasNext()) { nameRead = i
🌐
Educative
educative.io › answers › how-to-add-data-to-a-json-file-in-javascript
How to add data to a JSON file in JavaScript
Write the updated JavaScript object back to the JSON file using the writeFileSync() method as follows: fs.writeFileSync('data.json', JSON.stringify(jsonData)); Let's run the following widget and update the record of the data.json file by adding some data to it.
Find elsewhere
🌐
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"} ...
🌐
ArduinoJson
arduinojson.org › version 6 › how to's › how to append a json object to a file?
How to append a JSON object to a file? | ArduinoJson 6
Usually, we store a list of objects in a JSON array, like so: [ {"first_name":"Stan","last_name":"Marsh"}, {"first_name":"Kyle","last_name":"Broflovski"}, {"first_name":"Eric","last_name":"Cartman"} ] As you can see, appending an element to the array requires inserting the new object before the closing bracket (]).
🌐
SitePoint
sitepoint.com › javascript
Adding items to a JSON object - JavaScript
March 3, 2009 - hi, Does anyone know the function for adding more items to an already declared JSON object? Assuming you have something like: var event= [{}]; // this is the correct empty JSON declaration how can i add an itemt to the object? I used push but not the right function. Thanks
🌐
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 bracket notation:"); jsonObject.members.viewers['userName3'] = 'userData3'; console.log("\nJSON object after adding property:"); console.log(jsonObject);
🌐
Postman
community.postman.com › help hub
Add an object to existing json - Help Hub - Postman Community
January 29, 2021 - I want to add an object(key value pair) to a payload that i am getting from the external data file. “payload”:"{“key1”:“value1”,“key2”:“value2”,“key3”:“value3”}" What I want to do is add (“key4”:“value4”) to the payloa…
Top answer
1 of 12
105

json might not be the best choice for on-disk formats; The trouble it has with appending data is a good example of why this might be. Specifically, json objects have a syntax that means the whole object must be read and parsed in order to understand any part of it.

Fortunately, there are lots of other options. A particularly simple one is CSV; which is supported well by python's standard library. The biggest downside is that it only works well for text; it requires additional action on the part of the programmer to convert the values to numbers or other formats, if needed.

Another option which does not have this limitation is to use a sqlite database, which also has built-in support in python. This would probably be a bigger departure from the code you already have, but it more naturally supports the 'modify a little bit' model you are apparently trying to build.

2 of 12
54

You probably want to use a JSON list instead of a dictionary as the toplevel element.

So, initialize the file with an empty list:

with open(DATA_FILENAME, mode='w', encoding='utf-8') as f:
    json.dump([], f)

Then, you can append new entries to this list:

with open(DATA_FILENAME, mode='w', encoding='utf-8') as feedsjson:
    entry = {'name': args.name, 'url': args.url}
    feeds.append(entry)
    json.dump(feeds, feedsjson)

Note that this will be slow to execute because you will rewrite the full contents of the file every time you call add. If you are calling it in a loop, consider adding all the feeds to a list in advance, then writing the list out in one go.

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.

🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-add-new-property-to-json-object-using-javascript.php
How to Add New Property to JSON Object Using JavaScript
// Store JSON string in a JS variable var json = '{"name": "Harry", "age": 18}'; // Converting JSON-encoded string to JS object var obj = JSON.parse(json); // Adding a new property using dot notation obj.gender = "Male"; // Adding another property using square bracket notation obj["country"] = "United States"; // Accessing individual value from JS object document.write(obj.name + "<br>"); // Prints: Harry document.write(obj.age + "<br>"); // Prints: 14 document.write(obj.gender + "<br>"); // Prints: Male document.write(obj.country + "<br>"); // Prints: United States // Converting JS object back to JSON string json = JSON.stringify(obj); document.write(json);
🌐
UiPath Community
forum.uipath.com › help › studio
How to append JSON Object in another File - Studio - UiPath Community Forum
June 13, 2024 - Hi Team, I have two Json File , One for DEV and One for PROD, Basically i am trying to append “XYZ” from DEV JSON file to PROD file, Could you please help me here , Attached is the JSON files When ever new App code a…
🌐
Make Community
community.make.com › questions
Add a new value to an existing JSON - Questions - Make Community
June 28, 2023 - Hi, I am trying to add a value that I always need to add to an existing JSON. The thing is that my JSON is created and parsed, but I haven’t had any success using the module “Text aggregator” I need to add a new val…
🌐
GeeksforGeeks
geeksforgeeks.org › append-to-json-file-using-python
Append to JSON file using Python - GeeksforGeeks
March 26, 2024 - It means that a script (executable) ... JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script....
🌐
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 - Example: 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.