Excuse me for this simple problem - but I seem to miss something obvious.Any pointer would be a great help.
I have a JSON like
var whatever = [{
"key1" : { "text" : "text1","group" : "1" },
"key2" : { "text" : "text2","group" : "2" },
"key3" : { "text" : "text3","group" : "3" }
}];
I am trying to add another object(at start preferably) - but just couldn't get it to work.
var str = '{"text":"text0","group":"0"}';
var obj = JSON.parse(str);
whatever[0].put("key0",obj);
Getting the below error:
Uncaught TypeError: whatever[0].put is not a function
fiddle
JSON array can store values of type string, array, boolean, number, object, or null. In JSON array, values are separated by commas.
A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.
Core Java bootcamp program with Hands on practice We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.
You need to load the file, then parse it using var val = JSON. parse(textFromFile) you can then use push to push it onto the end of that val val. push({/*new object*/}) , then you will need to stringify the val var newString = JSON. strigify(val) and then you finally can save newString to a file.
There is no put
function on the object. Use property instead of it. When you want to assign to a property which does not exist, it creates a new one and assigns the value to it.
whatever[0]["key0"] = obj;
What is related to at start preferably, there is no order for object properties. It is a wrong statement. If you want ordering try to think from the view of array of objects instead of array of object, which contains objects.
Code examples
const whatever = [{
"key1" : { "text" : "text1","group" : "1" },
"key2" : { "text" : "text2","group" : "2" },
"key3" : { "text" : "text3","group" : "3" }
}];
const str = '{ "text" : "text0", "group" : "0" }';
const obj = JSON.parse(str);
whatever[0]["key0"] = obj;
console.log(whatever);
Or use Object#assign
const whatever = [{
"key1" : { "text" : "text1","group" : "1" },
"key2" : { "text" : "text2","group" : "2" },
"key3" : { "text" : "text3","group" : "3" }
}];
const str = '{ "text" : "text0", "group" : "0" }';
const obj = JSON.parse(str);
Object.assign(whatever[0], { key0: obj }) // this will also change the object
console.log(whatever);
My suggestion is to use an array of objects, if you want something with order.
const whatever = [
{ "text" : "text1","group" : "1" },
{ "text" : "text2","group" : "2" },
{ "text" : "text3","group" : "3" }
];
const str = '{ "text" : "text0", "group" : "0" }';
const obj = JSON.parse(str);
// Add to the start
whatever.unshift(obj);
console.log(whatever);
// Add to the end
whatever.push(obj);
console.log(whatever);
maybe you want something like this
var whatever = [{
"key1" : { "text" : "text1","group" : "1" },
"key2" : { "text" : "text2","group" : "2" },
"key3" : { "text" : "text3","group" : "3" }
}];
Object.assign(whatever[0], {key4 : { "text" : "text4","group" : "4" }});
console.log(whatever);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With