0

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"
 }]
kasper Taeymans
7,0365 gold badges34 silver badges51 bronze badges
asked Feb 5, 2015 at 15:08
2
  • 1
    Can you show us more of your code? Commented Feb 5, 2015 at 15:09
  • try tempobj["info"] = {"name":"title"} if you want to add the info to the tempobj but if you want to add it a a specific index, tempobj[iIndex]["info"] = {"name":"title"} Commented Feb 5, 2015 at 15:15

2 Answers 2

2

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));
answered Feb 5, 2015 at 15:15
3
  • can you post sample js code to achieve this structure..I am getting json parse error Commented Feb 5, 2015 at 15:36
  • I added a sample, and I modified the first structure to be JSON compliant (this was a javascript object before). Commented Feb 5, 2015 at 16:04
  • Glad if I could help you! Commented Feb 5, 2015 at 20:23
0

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);
answered Feb 5, 2015 at 15:13
2
  • why did you convert this to single array ? I want it to be 2 Commented Feb 5, 2015 at 15:35
  • @Vishnu: Because it wasn't at all clear how or why they were two. The question lacks clarity throughout. The above shows all the fundamentals you need, if you need two arrays, adjust accordingly. Commented Feb 5, 2015 at 15:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.