llerdal
llerdal

Reputation: 387

How to get second level Json value

I am trying to iterate over the second level json values in a response.

This is a sample of the Json:

  {

  "Links": {},

 "Items": [

  {

  "WebId": "A0EUfSms148rEStZ8_fh",

  "Name": "AA to Slurry ",

  "Path": "klj",

  "Links": {

    "Source": ""

  },

  "Items": [

    {

      "Timestamp": "2016-08-21T05:14:27.0180053Z",

      "Value": 0.0154830571,

      "UnitsAbbreviation": "%",

      "Good": true,

      "Questionable": false,

      "Substituted": false

    },

    {

      "Timestamp": "2016-08-21T05:14:30Z",

      "Value": 0.0155982981,

      "UnitsAbbreviation": "%",

      "Good": true,

      "Questionable": false,

      "Substituted": false

    },

    {

      "Timestamp": "2016-08-21T05:14:30Z",

      "Value": 0.0155982981,

      "UnitsAbbreviation": "%",

      "Good": true,

      "Questionable": false,

      "Substituted": false

    },

    {

      "Timestamp": "2016-08-21T05:14:33.024002Z",

      "Value": 0.0155704552,

      "UnitsAbbreviation": "%",


  "Items": [

{

  "WebId": "A0EUfSms148rEStZ8_fh",

  "Name": “Slurry ",

  "Path": "klj",

  "Links": {

    "Source": ""

  },

  "Items": [

    {

      "Timestamp": "2016-08-21T05:14:27.0180053Z",

      "Value": 0.0154830571,

      "UnitsAbbreviation": "%",

      "Good": true,

      "Questionable": false,

      "Substituted": false

    },

    {

      "Timestamp": "2016-08-21T05:14:30Z",

      "Value": 0.0155982981,

      "UnitsAbbreviation": "%",

      "Good": true,

      "Questionable": false,

      "Substituted": false

    },

    {

      "Timestamp": "2016-08-21T05:14:30Z",

      "Value": 0.0155982981,

      "UnitsAbbreviation": "%",

      "Good": true,

      "Questionable": false,

      "Substituted": false

    },

    {

      "Timestamp": "2016-08-21T05:14:33.024002Z",

      "Value": 0.0155704552,

      "UnitsAbbreviation": "%",

This is the code I have so far that returns only the name:

  var feat = resp.Items,
        tableData = [];

    // Iterate over the JSON object
    for (var i = 0, len = feat.length; i < len; i++) {
        tableData.push({
            "name": feat[i].Name,
            "Timestamp": feat[i].Timestamp,
            "value": feat[i].Items.Value,

        });
    }

So in this example this example tabledata would ideally look like this:

name        | Timestamp | value
AA to Slurry  
AA to Slurry
AA to Slurry
AA to Slurry
AA to Slurry
AA to Slurry 
Slurry 
Slurry
Slurry
Slurry
Slurry
Slurry

With the corresponding timestamp and value from the second level "Items". Right now all I get is the name value how do I get the name then iterate over the second level items?

Upvotes: 0

Views: 4562

Answers (1)

Niles Tanner
Niles Tanner

Reputation: 4041

I think your problem is that your sub Items array is being accessed directly. (feat[i].Items.Value) instead you should loop through 2nd array as well. I put together this code to get you started, it loops through the items in the json you have then loops through the items inside each item of the first array.

json.Items.forEach(function(item){
  item.Items.forEach(function(subItem){
    tableData.push({
      'name':item.Name,
      'timestamp':item.TimeStamp,
      'value':subItem.Value
    });
  });
});

Upvotes: 3

Related Questions