Link to home
Start Free TrialLog in
Avatar of ray-solomon
ray-solomonFlag for United States of America

asked on

Add new row to empty JSON object

I would like to simply add the following properties into this object. But this code does not work.

var JSON_object = [{}]; // not sure if this is proper syntax for empty JSON object
JSON_object[JSON_object.length] = {"day":"", "element_id":"d164", "id":"1"};


The object is supposed to be populated like this:
[{"day":"", "element_id":"d164", "id":"1"}]

Avatar of numberkruncher
numberkruncher
Flag of United Kingdom of Great Britain and Northern Ireland image

Hope the following is of help:
// Empty JSON object:

var obj = {};


// JSON object with several attributes:

var obj = {
    "emptyArray": [],
    "anArray": [ 1, 2, 3 ],
    "nestedObject": {
        "aString": "Hello World!"
    },
    "anInteger": 24
};


// Your Example:

var obj = {
    "day": "",
    "element_id": "d164",
    "id": 1
};

Open in new window

Another little example:
// An array of objects:

var obj = {
    "nestedObjects": [
        {
            "id": 1,
            "name": "Bob"
        },
        {
            "id": 2,
            "name": "Jessica"
        }
    ]
};

Open in new window

Avatar of ray-solomon

ASKER

My object will have multiple rows of data and I think your example does not help.

Here is an example of what it can look like.
var JSON_object = [
      {"day":41,"element_id":"d156","id":50},
      {"day":42,"element_id":"d157","id":51},
      {"day":43,"element_id":"d158","id":52},
      {"day":44,"element_id":"d159","id":53}];

So using your example, this should have worked, but it doesn't.
var JSON_object = {};
JSON_object = {"day":"", "element_id":"d164", "id":"1"};

Why?

Help me make this work.
From my understanding JSON shares that it must have a single root object like in XML.

So if you want to store an array then you need to put it inside a root object:
var JSON_object = {

   "array": [
      {"day":41,"element_id":"d156","id":50},
      {"day":42,"element_id":"d157","id":51},
      {"day":43,"element_id":"d158","id":52},
      {"day":44,"element_id":"d159","id":53}
   ]

};

Open in new window

My entire application works wonderfully from a JSON object with the way I pointed out.
However, my problem is that when I start out with no rows in the object, I am unable to add any rows to the object.


Here is a previous question that helped me add to the object when it already contained existing rows.
https://www.experts-exchange.com/questions/26695826/Add-item-to-json-object.html


Here is how I delete from the object:
https://www.experts-exchange.com/questions/26695917/Delete-item-from-json-object.html
ASKER CERTIFIED SOLUTION
Avatar of numberkruncher
numberkruncher
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for helping.
I got it figured out.
:)
No problem! Glad that I could help!