Home > other >  Get field of JSON in Javascript Console
Get field of JSON in Javascript Console

Time:10-04

I have a JSON like this, how to get the value of StatusDescription? I tried many times but the result is undefined. Here is my JSON:

 {
  "meta": {
    "a2": 200,
    "ta": "dasd",
    "asdd": "asdda"
  },
  "data": {
    "items": [
      {
        "id": "",
        "number": "",
        "origin_info": {
          "ItemReceived": "2021-10-02 02:07:49",
          "phone": 123456789,
          "trackinfo": [
            {
              "StatusDescription": "what i need",
              "Details": "",
              "substatus": "ok"
            },
   
            {
              "StatusDescription": "what i need",
              "Details": "",
              "substatus": "ok"
            }
          ]
        },
        "destination_info": null,
        "lastEvent": "grgrgrgrgr",
        "lastUpdateTime": "mewmemew"
      }
    ]
  }
}

I'm using in my NodeJS app, like myapp.js, and console.log()

CodePudding user response:

Try this

I stored your sample json in variable json

var json = {
  "meta": {
    "a2": 200,
    "ta": "dasd",
    "asdd": "asdda"
  },
  "data": {
    "items": [
      {
        "id": "",
        "number": "",
        "origin_info": {
          "ItemReceived": "2021-10-02 02:07:49",
          "phone": 123456789,
          "trackinfo": [
            {
              "StatusDescription": "what i need",
              "Details": "",
              "substatus": "ok"
            },
   
            {
              "StatusDescription": "what i need",
              "Details": "",
              "substatus": "ok"
            }
          ]
        },
        "destination_info": null,
        "lastEvent": "grgrgrgrgr",
        "lastUpdateTime": "mewmemew"
      }
    ]
  }
}

Accessed it like below

console.log(json.data.items[0].origin_info.trackinfo[0].StatusDescription);

Items is an array and we took array element 0.

trackinfo again is an array and we took array element 0.

We can change array index or loop through and get required values.

CodePudding user response:

You have to iterate through your items and trackinfo to get to StatusDescription. Try this one.

const data =  {
  "meta": {
    "a2": 200,
    "ta": "dasd",
    "asdd": "asdda"
  },
  "data": {
    "items": [
      {
        "id": "",
        "number": "",
        "origin_info": {
          "ItemReceived": "2021-10-02 02:07:49",
          "phone": 123456789,
          "trackinfo": [
            {
              "StatusDescription": "what i need",
              "Details": "",
              "substatus": "ok"
            },
   
            {
              "StatusDescription": "what i need",
              "Details": "",
              "substatus": "ok"
            }
          ]
        },
        "destination_info": null,
        "lastEvent": "grgrgrgrgr",
        "lastUpdateTime": "mewmemew"
      }
    ]
  }
}

const items = data.data.items.map(item => item)
const trackinfo = items.map(item => item.origin_info.trackinfo).flat()
console.log(trackinfo)
const statusDescription = trackinfo.map(trackinfo => trackinfo.StatusDescription)

console.log(statusDescription)

  • Related