Vishnu
Vishnu

Reputation: 2452

Adding extra values in json object

For Example

I have Object named tempobj and below are tempobj[0] and tempobj[1] sample data.

I want to add extra info like name and status this object

tempobj ["info"]["name"] = "title";
tempobj ["info"]["id"] = "23243";

But when i do stringify , Jquery is ignoring this value...how can i add data like this to this kind of structure.

 [
        [{
            "name": "Breakfast",
            "value": "buffalo strip ",
            "check": 0
        }, {
            "name": "snack ",
            "value": "pecan pie butter",
            "check": 0
        }, {
            "name": "dessert",
            "value": "chocolate zucchani brownie",
            "check": 0
        }],
        [{
            "name": "Breakfast",
            "value": "Stir Fried Kale and Baccon  ",
            "check": 1
        }, {
            "name": "snack ",
            "value": "Smoked Salmon nori roll  "
        }, {
            "name": "dessert",
            "value": "Apple muffins"
        }]

Upvotes: 0

Views: 2031

Answers (2)

Gnucki
Gnucki

Reputation: 5133

tempobj is an array ([]).

When you try to set some values with:

tempobj["info"]["name"] = "title";
tempobj["info"]["id"] = "23243";

you treat it like an object ({}).

If you want to add some data to your tempobj, you have to change its structure like this for example:

{
    "info": {
        "name": "title",
        "id": "23243"
    },
    "items": [
        [{
            "name": "Breakfast",
            "value": "buffalo strip ",
            "check": 0
        }, {
            "name": "snack ",
            "value": "pecan pie butter",
            "check": 0
        }, {
            "name": "dessert",
            "value": "chocolate zucchani brownie",
            "check": 0
        }],
        [{
            "name": "Breakfast",
            "value": "Stir Fried Kale and Baccon  ",
            "check": 1
        }, {
            "name": "snack ",
            "value": "Smoked Salmon nori roll  "
        }, {
            "name": "dessert",
            "value": "Apple muffins"
        }]
    ]
}

Here is a sample:

var tempobj = {
    items: [
        [{
            "name": "Breakfast",
            "value": "buffalo strip ",
            "check": 0
        }, {
            "name": "snack ",
            "value": "pecan pie butter",
            "check": 0
        }, {
            "name": "dessert",
            "value": "chocolate zucchani brownie",
            "check": 0
        }],
        [{
            "name": "Breakfast",
            "value": "Stir Fried Kale and Baccon  ",
            "check": 1
        }, {
            "name": "snack ",
            "value": "Smoked Salmon nori roll  "
        }, {
            "name": "dessert",
            "value": "Apple muffins"
        }]
    ]
}

tempobj.info = {
    name: 'title',
    id: '23243'
};

// Another way:
//tempobj.info = {};
//tempobj.info.name = 'title';
//tempobj.info.id = '23243';

console.log(JSON.stringify(tempobj));

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074305

It sounds like you're doing something like this:

// Create an array
var a = [];

// Add an entry to it
a[0] = "I'm an array entry";

// Add a non-entry property to it
a.foo = "bar";

// Convert to JSON
var json = JSON.stringify(a);

...and finding that the string doesn't have the foo property.

That's correct. JSON's arrays don't support arbitrary non-entry properties the way JavaScript's arrays do, and so they get silently dropped during serialization (the same way properties with the value undefined or referring to functions do).

The answer is not to do that if you need to go through JSON.

But, the structure you quoted later in your question doesn't do that, and can readily be created like this:

var data = [
    [
        {
            "name": "Breakfast",
            "value": "buffalo strip ",
            "check": 0
        },
        {
            "name": "snack ",
            "value": "pecan pie butter",
            "check": 0
        },
        {
            "name": "dessert",
            "value": "chocolate zucchani brownie",
            "check": 0
        }
    ],
    [
        {
            "name": "Breakfast",
            "value": "Stir Fried Kale and Baccon  ",
            "check": 1
        },
        {
            "name": "snack ",
            "value": "Smoked Salmon nori roll  "
        },
        {
            "name": "dessert",
            "value": "Apple muffins"
        }
    ]
];

var json = JSON.stringify(data);

That structure is (from the outside in): An array containing two entries, each of which is another array; within each array, you have a series of objects, which have properties.

I don't know why you want to have the two arrays inside the outermost array, but that's what the structure showed.

If you meant just a single array, that would look like this if you were creating it all at once:

var data = [
    {
        "name": "Breakfast",
        "value": "buffalo strip ",
        "check": 0
    },
    {
        "name": "snack ",
        "value": "pecan pie butter",
        "check": 0
    },
    {
        "name": "dessert",
        "value": "chocolate zucchani brownie",
        "check": 0
    },
    {
        "name": "Breakfast",
        "value": "Stir Fried Kale and Baccon  ",
        "check": 1
    },
    {
        "name": "snack ",
        "value": "Smoked Salmon nori roll  "
    },
    {
        "name": "dessert",
        "value": "Apple muffins"
    }
];

var json = JSON.stringify(data);

...or if building it up over time:

var data = [];
data.push({
    name: "Breakfast",
    value: "buffalo strip ",
    check: 0
});
data.push({
    name: "snack ",
    value: "pecan pie butter",
    check: 0
});
// ...

var json = JSON.stringify(data);

Upvotes: 0

Related Questions