1

An ajax request (stored in a variable named results) is returning this data as a response:

Object {hits: Object, links: Object}
  hits:Object
    hits:Array(2)
      0:Object
         active:true
         email:"user1@example.com"
         id:1
         links:Object
         __proto__:Object
      1:Object
         active:true
         email:"user2@example.com"
         id:2
         links:Object
         __proto__:Object
      length:2
      __proto__:Array(0)
      total:2
    __proto__:Object
  links:Object
__proto__:Object

What sort of data type it has? I thought it is json but using JSON.parse(results) returns this error:

 Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)

How can I get access to the Array inside it? I need to get the email addresses and ids. It's probably not relevant but I'm using it in a ReactJS component.

Birish
  • 5,514
  • 5
  • 32
  • 51
  • It's already an object so you don't need to parse it. Use JSON.stringify if you want to get a string representation of the object. – user2085143 Feb 21 '17 at 17:31
  • 2
    Learn what JSON is (and is not) at json.org – Dexygen Feb 21 '17 at 17:31
  • How do you access anything in an object? `objectReference.property`... – Heretic Monkey Feb 21 '17 at 17:36
  • Like 2085143 said, It is already an Json object. Json can be presented in two ways, one is json string on which you can call json.parse(), and the other one is json object on which you can call json.stringfy(). In your example, it looks the latter example. – user3207158 Feb 21 '17 at 17:36
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Heretic Monkey Feb 21 '17 at 17:37
  • ok you all are right. It is already a json object. So I tried to access the values but it fails. `results[‘hits']['hits']['0']`, `results['hits']['hits'][0]` or `results['hits'].hits.email` all fails with this error: ` uncaught TypeError: Cannot read property 'hits' of undefined` . It recognizes the first `hits`, but not the second one – Birish Feb 21 '17 at 18:34
  • @Sarah did you checked my answer ? – Debug Diva Feb 22 '17 at 18:28

2 Answers2

0

According your response, it's already a JSON object. So if you want to access the hits property, here is the code: results.hits.

Also you mentioned the following:

It recognizes the first hits, but not the second one.

Can you please try to console.log(results.hits) and share the output here? I guess your response object has only one hits property and it's type is array.

Jordan Enev
  • 16,904
  • 3
  • 42
  • 67
0

Observation : As response is already a JSON Object so no need to Parse it again.

Suggestion : Use array.map() method to iterate the array and fetch the required elements.

DEMO

var results = {
  "hits": {
  "hits": [
    {
      active:true,
      email:"user1@example.com",
      id:1
    },
    {
      active:true,
      email:"user2@example.com",
      id:2
    }
  ]
  },
  "links": {}
};

var res = results.hits.hits.map(function(item) {
  return item.email;
});

console.log(res);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123