0

I am completely new to Node.js. I have to read specific value from a API responded Nested JSON. I have tried to figure out but getting Undefined instead of the value. Whenever I am trying to read the base object I am getting the responded value but whenever I am trying to read the object under array then I am getting Undefined value.

Code Snippet:

var https = require('https');
var optionget = {
  host: 'api-dev.dataoverflow.com',
  port: 443,
  path: '/test1/test2/',
  method: 'GET',
  HEADERS: {
    'Authorization': 'Basic grege==',
    'X-PruAppID : '
    PlainCardPortal '
  }
};

console.info(optionsget)

var reqGet = htttps.request(optionsget, function(res) {
  console.log("statusCode: ", res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
    process.stdout.write(jsonobj);
    var Value = jsonobj.items.item.topping.type;
    console.log(Value)
  });
});

reqGet.end();
reqGet.on('error', function(e) {
  console.error(e);
});

var optionsgetmsg = {
  host: 'api-dev.dataoverflow.com',
  port: 443,
  method: 'GET'
};

console.info(optionsgetmsg);

var reqGet = https.request(optionsget, function(res) {
  console.log("statusCode: ", res.statusCode);
  res.setEncoding('utf-8')

  res.on('data', function(data) {
    process.stdout.write(data);
  });
});

reqGet.end();
reqGet.on('error', function(e) {
  console.error(e);
});

Nested JSON Snippet:

{
    "items":
        {
            "item":
                [
                    {
                        "id": "0001",
                        "type": "donut",
                        "name": "Cake",
                        "ppu": 0.55,
                        "batters":
                            {
                                "batter":
                                    [
                                        { "id": "1001", "type": "Regular" },
                                        { "id": "1002", "type": "Chocolate" },
                                        { "id": "1003", "type": "Blueberry" },
                                        { "id": "1004", "type": "Devil's Food" }
                                    ]
                            },
                        "topping":
                            [
                                { "id": "5001", "type": "None" },
                                { "id": "5002", "type": "Glazed" },
                                { "id": "5005", "type": "Sugar" },
                                { "id": "5007", "type": "Powdered Sugar" },
                                { "id": "5006", "type": "Chocolate with Sprinkles" },
                                { "id": "5003", "type": "Chocolate" },
                                { "id": "5004", "type": "Maple" }
                            ]
                    },

                    ...

                ]
        }
}

I want to read type : Glazed from this JSON only but I am getting Undefined.

Matt Grow
  • 1
  • 1
  • `topping` is an array, so you need to get a specific index from it to get `type` from it. – VLAZ Mar 18 '19 at 06:50
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Anurag Srivastava Mar 18 '19 at 06:50

1 Answers1

1

You have to iterate over the values in an array. You cannot access them using . notation. Use map to iterate over the array and get they types in the topping array

var jsonobj = {
  "items": {
    "item": [{
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters": {
          "batter": [{
              "id": "1001",
              "type": "Regular"
            },
            {
              "id": "1002",
              "type": "Chocolate"
            },
            {
              "id": "1003",
              "type": "Blueberry"
            },
            {
              "id": "1004",
              "type": "Devil's Food"
            }
          ]
        },
        "topping": [{
            "id": "5001",
            "type": "None"
          },
          {
            "id": "5002",
            "type": "Glazed"
          },
          {
            "id": "5005",
            "type": "Sugar"
          },
          {
            "id": "5007",
            "type": "Powdered Sugar"
          },
          {
            "id": "5006",
            "type": "Chocolate with Sprinkles"
          },
          {
            "id": "5003",
            "type": "Chocolate"
          },
          {
            "id": "5004",
            "type": "Maple"
          }
        ]
      }
    ]
  }
}
var Value = jsonobj.items.item.map(e=>e.topping)[0].map(x=>x.type);
console.log(...Value)
ellipsis
  • 12,049
  • 2
  • 17
  • 33