Uidev123
Uidev123

Reputation: 83

How to access JSON property?

I am having trouble in accessing JSON property by key value.

This is my JSON and i would like to access overSerializedItems and underSerializedItems as string format but not able to display any data

response = [{
    "storeId": "2011",
    "overSerializedItems": [{
        "overshipid": "8901260932784148868F"
      },
      {
        "overshipid": "8901260145723866348F"
      },
    ],
    "underSerializedItems": [],
  },
  {
    "storeId": "2011",
    "overSerializedItems": [],
    "underSerializedItems": [{
        "undershipid": "89"
      },
      {
        "undershipid": "81"
      },
    ],
  }
]

I am trying following approach but getting error 'Can not read property tostring() of undefined'

 for(const item of response) {
  const json = {
      'Store #': item.storeId,
      'Overshipment': item.overSerializedItems !== null ? 
       item.overSerializedItems.overshipid.toString() : [], //Here values i should get 8901260932784148868F,8901260145723866348F
       'Undershipment':item.underSerializedItems !== null ? 
       item.underSerializedItems.undershipid.toString() : [] // Here values i should get are 89,81 
     }  
}

Upvotes: 0

Views: 445

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075567

You're using !== null to check whether overSerializeItems and/or underSerializedItems are absent from the response. That check will be false, because if they're absent, you get undefined, not null, when you try to access them on the object.

In both of those cases, since they're either absent or objects, just use an item.overSerializedItems ? guard:

for (const item of response) {
  const obj = { // not JSON
    'Store #': item.storeId,
    'Overshipment': item.overSerializedItems ?
      item.overSerializedItems : [],
    'Undershipment': item.underSerializedItems ?
      item.underSerializedItems : []
  }
  console.log(obj);
}

or if you're supply a default [] if the array isn't present, use ||:

for (const item of response) {
  const obj = { // not JSON
    'Store #': item.storeId,
    'Overshipment': item.overSerializedItems || [],
    'Undershipment': item.underSerializedItems || []
  }
  console.log(obj);
}

Also note that there's no point to .toString() on overshipid and undershipid; they're already strings. But I've removed that anyway, since you were trying to use .overshipid and .undershipid on arrays; they don't exist as properties on the arrays, they exist as properties of the objects within the arrays. (Removing them also makes the [] default there make more sense.)

Live Example:

const response = [{
    "storeId": "2011",
    "overSerializedItems": [{
        "overshipid": "8901260932784148868F"
      },
      {
        "overshipid": "8901260145723866348F"
      },
    ],
    "underSerializedItems": [],
  },
  {
    "storeId": "2011",
    "overSerializedItems": [],
    "underSerializedItems": [{
        "undershipid": "89"
      },
      {
        "undershipid": "81"
      },
    ],
  }
];

for (const item of response) {
  const obj = { // not JSON
    'Store #': item.storeId,
    'Overshipment': item.overSerializedItems || [],
    'Undershipment': item.underSerializedItems || []
  }
  console.log(obj);
}
.as-console-wrapper {
  max-height: 100% !important;
}

Upvotes: 1

Related Questions