Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to insert object into array of json object

Tags:

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

like image 934
Soumya Avatar asked Sep 22 '17 08:09

Soumya


People also ask

Can JSON array contain object?

JSON array can store values of type string, array, boolean, number, object, or null. In JSON array, values are separated by commas.

How do you write an array of objects in JSON?

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.

How do you add an object to a JSON array in Java?

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.

How do I add an object to a JSON file?

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.


2 Answers

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);
like image 152
Suren Srapyan Avatar answered Oct 11 '22 14:10

Suren Srapyan


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);
like image 26
marvel308 Avatar answered Oct 11 '22 16:10

marvel308