0

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 
     }  
}
Uidev123
  • 73
  • 3
  • 13
  • 1
    There's no JSON in your question. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript (or TypeScript) source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Sep 04 '18 at 15:14
  • _What does the error say_? – SLaks Sep 04 '18 at 15:16
  • 2
    `item.overSerializedItems` is an array, you can't access the `overshipid` property of it – Luca Kiebel Sep 04 '18 at 15:16
  • Getting error can not read property tostring() of undefined – Uidev123 Sep 04 '18 at 15:17
  • Yeah, you are getting that array, because you have to first access elements of the array in order to access `overshipid` of them – Luca Kiebel Sep 04 '18 at 15:19

1 Answers1

1

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;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the response i tried this approach but i am getting [object object] how to display the undershipid and overshipid values? – Uidev123 Sep 04 '18 at 15:26
  • @Uidev123 - (The above won't show you `[object Object]` on a standard console.) You have to do something with the array they're in. What that is will depend on what you want to do. – T.J. Crowder Sep 04 '18 at 15:30