Home > database >  Output json stringify elements
Output json stringify elements

Time:07-26

I have a problem. I am using TypeScript and I get a response back. I want to output the information. How could I output e.g. internalCompanyCode and timestamp.

What I got

Element implicitly has an 'any' type because index expression is not of type 'number'.(
//const json = JSON.stringify(result, null, 2)
// json is the result of JSON.stringify(...)
const json = [
  {
    "internalCompanyCode": "007",
    "moreInfo": [
      {
        "dimensions": {
          "height": 27,
          "width": 31,
          "length": 61
        },
        "currenStatus": {
          "arrived": {
            "timestamp": "10:00:12"
          }
        }
      }
    ]
  }
]

console.log(json['internalCompanyCode'])
console.log(json['moreInfo']['dimensions']['height'])

CodePudding user response:

To get data you don't have to stringify result. You have to use result before stringifying to json or have to parse json back to result if you did it

console.log(result[0]['internalCompanyCode']) //007

console.log(result[0]['moreInfo'][0]['currenStatus']['arrived']['timestamp']) // 10:00:12

console.log(result[0]['moreInfo'][0]['dimensions']['height']) //27

CodePudding user response:

Your json object is actually a list so you will have to access it like json[0]['internalCompanyCode']

  • Related