Khan
Khan

Reputation: 271

How to access the nested JSON objects

I want to access nested JSON object values by iterating.

I tried console.log(test[0].Invoice[0].Cost.NO[0]); but it's not working.

var test = [{
  "Invoice": {
    "NO": "869",
    "$$hashKey": "object:186"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698",
    "$$hashKey": "object:189"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

console.log(test[0].Invoice[0].Cost.NO[0]);

Upvotes: 0

Views: 144

Answers (5)

Pugazh
Pugazh

Reputation: 9561

Your code console.log(test[0].Invoice[0].Cost.NO[0]); won't work.

Because Invoice and Cost are same level objects (Cost isn't nested in Invoice).

Check below example on how to access them.

You would need to understand Arrays [] and Objects {}.

var test = [{
  "Invoice": {
    "NO": "869",
    "$$hashKey": "object:186"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698",
    "$$hashKey": "object:189"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

// To access COST
console.log(test[0].Cost[0].NO);

// To access Invoice
console.log(test[0].Invoice.NO);

Upvotes: 1

Amol B Lande
Amol B Lande

Reputation: 252

 var test = [{ "Invoice": { "NO": "869", "$$hashKey": "object:186" }, "Cost": [{ "NO": 183, "Amnt": 100 }, { "NO": 184, "Amnt": 200 }] }, { "Invoice": { "NO": "698", "$$hashKey": "object:189" }, "Cost": [{ "NO": 110, "Amnt": 150 }, { "NO": 142, "Amnt": 263 }] }];
        for (var t in test)
        {
            var obj = test[t];
            console.log(obj.Invoice.NO);
            console.log(obj.Invoice.$$hashKey);
            for ( var cst in obj.Cost)
            {
                console.log("Cost Value: " + obj.Cost[cst].NO + " " + obj.Cost[cst].Amnt);
            }
        }

Upvotes: 0

Dhiraj
Dhiraj

Reputation: 1462

You have written wrong statement

test[0].Invoice[0].Cost.NO[0]

It should be as below

 test[0].Cost[0].NO

Because Invoice and Cost are properties of same json object.

try out this

  var test = [{ "Invoice": { "NO": "869", "$$hashKey": "object:186" }, "Cost": [{ "NO": 183, "Amnt": 100 }, { "NO": 184, "Amnt": 200 }] }, { "Invoice": { "NO": "698", "$$hashKey": "object:189" }, "Cost": [{ "NO": 110, "Amnt": 150 }, { "NO": 142, "Amnt": 263 }] }];
 

  console.log(test[0].Cost[0]);
 console.log(test[0].Cost[0].NO);
   

 

Upvotes: 0

Weedoze
Weedoze

Reputation: 13943

test is an array thus you need to iterate over it.

Each iteration will give you an object with 2 properties : Invoice and Cost

Invoice is again an object thus you only need to access the property you need

Cost is an array thus you need to iterate each items which are objects

var test = [{
  "Invoice": {
    "NO": "869",
    "$$hashKey": "object:186"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698",
    "$$hashKey": "object:189"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

test.forEach(t=>{
  console.log("Invoice N°: " + t.Invoice.NO);
  t.Cost.forEach(c=>{
    console.log("- Cost N°: "+ c.NO + " - Amount: " + c.Amnt);
  });
});

Upvotes: 0

hering
hering

Reputation: 1954

This is your JSON:

var test = [{
  "Invoice": {
    "NO": "869"
  },
  "Cost": [{
    "NO": 183,
    "Amnt": 100
  }, {
    "NO": 184,
    "Amnt": 200
  }]
}, {
  "Invoice": {
    "NO": "698"
  },
  "Cost": [{
    "NO": 110,
    "Amnt": 150
  }, {
    "NO": 142,
    "Amnt": 263
  }]
}];

console.log(test[0].Cost[0].NO); would be 183

You can check your structure in development with console.log(test) if you are not used to JSON.

To iterate you should use a loop as described here: https://www.w3schools.com/jsref/jsref_forEach.asp

Short example:

 var myFunction = function(item,index){
    console.log(item.Cost[0].No);
 }
 test.forEach(myFunction)

Upvotes: 0

Related Questions