0

I am trying to access key values, but it keeps saying the it is undefined.

I am trying to interact with this API that initially returns this:

 [ { 
    version: [ '2.11.0' ],
    timestamp: [ '2020-02-27T22:05:45.407Z' ],
    searchResult: [ [Object] ],
    paginationOutput: [ [Object] ]
  } ]

To access searchResult, I use the following:

console.log(data[0].searchResult[0]);

That prints out the following object:

{[
   { itemId: [Array],
      title: [Array],
   },
   { itemId: [Array],
      title: [Array],
   },
   { itemId: [Array],
     title: [Array],
   }
]}

However, when I try to access itemId array by its index as in the following, I keep getting undefined errors.

let result = data[0].searchResult[0] ;
console.log(result[0]);

Am I missing something?

Zeusox
  • 7,708
  • 10
  • 31
  • 60
  • 2
    Can you take another look at your JSON? The JSON that you provided is not in valid JSON format. – Jon Warren Feb 27 '20 at 21:56
  • 1
  • 1
    Since data is an array please try data[0].itemId[0] – trizin Feb 27 '20 at 21:57
  • Is the entire object stored on data variable ? – Henok Teklu Feb 27 '20 at 21:57
  • Are you parsing the JSON? `console.log(JSON.parse(data).itemId[0])` – Andy Mardell Feb 27 '20 at 21:57
  • At the moment, your JSON is invalid.. I have a feeling that your returned json is an array, if so, you can specify the element index of data to get it, ex: console.log(data[0].itemId[0]); – wakakak Feb 27 '20 at 21:58
  • How is Json is invalid? That is exactly what I get as a response! – Zeusox Feb 27 '20 at 21:59
  • `{[` is not valid - it's either an array or an object, not both. – stjns Feb 27 '20 at 22:01
  • it should have key as 'data' for array – Prakash Reddy Potlapadu Feb 27 '20 at 22:01
  • @Bodacious [`{[` is valid in ES6](https://stackoverflow.com/a/19837961/1913729). `{[...]}` is not. – blex Feb 27 '20 at 22:02
  • @blex Since {[ is valid, why it is not being parsed? – Zeusox Feb 27 '20 at 22:04
  • 1
    Because `{[` is only valid when used in JS to create a dynamic key in an Object: `{[myVar]: 42}`. That's not the case here (there is no value associated with it). And it's never valid in JSON. Also, JSON requires to have double quotes around keys (like `"itemId"`) – blex Feb 27 '20 at 22:04
  • @blex I will update my question – Zeusox Feb 27 '20 at 22:06
  • @blex updated my question. Hopefully that makes it a little bit more clear... – Zeusox Feb 27 '20 at 22:10
  • @Zeusox Try logging `JSON.stringify(data[0].searchResult[0], 0, 2);` instead. What you posted has been simplified by NodeJS – blex Feb 27 '20 at 22:11
  • @blex that works perfect. Is there anyway I could convert this into a loop. Maybe if you can post it so I mark it as the correct answer for others to learn from. THanks... – Zeusox Feb 27 '20 at 22:12
  • 1
    @Zeusox I don't know what is supposed to come after `data[0].searchResult[0]` (the data you posted has been simplified by NodeJS) – blex Feb 27 '20 at 22:16
  • @blex Could you please at least elaborate on what do you mean by simplified by Node? – Zeusox Feb 27 '20 at 22:17
  • 1
    Just like in Google Chrome's console, when you log a nested Object or Array, the console will show you a simplified/truncated version of it. For example, in what you posted, you can see it says `Array` instead of showing you what's inside. [Like here](https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object) – blex Feb 27 '20 at 22:23
  • @blex Thanks! looks like just using JSON.stringify(data[0].searchResult[0]) works. Not sure what was , 0, 2 for. – Zeusox Feb 27 '20 at 22:25
  • 1
    `0, 2` was just to pretty print it (adding spaces, line breaks and indentation instead of a single line) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – blex Feb 27 '20 at 22:27

0 Answers0