0

I'm running a node.js server that sends queries to an elasticsearch instance. Here is an example of the JSON returned by the query:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 9290,
        "max_score": 0,
        "hits": []
    },
    "suggest": {
        "postSuggest": [
            {
                "text": "a",
                "offset": 0,
                "length": 1,
                "options": [
                    {
                        "text": "Academic Librarian",
                        "score": 2
                    },
                    {
                        "text": "Able Seamen",
                        "score": 1
                    },
                    {
                        "text": "Academic Dean",
                        "score": 1
                    },
                    {
                        "text": "Academic Deans-Registrar",
                        "score": 1
                    },
                    {
                        "text": "Accessory Designer",
                        "score": 1
                    }
                ]
            }
        ]
    }
}

I need to create an array containing each job title as a string. I've run into this weird behavior that I can't figure out. Whenever I try to pull values out of the JSON, I can't go below options or everything comes back as undefined.

For example:

arr.push(results.suggest.postSuggest) will push just what you'd expect: all the stuff inside postSuggest.

arr.push(results.suggest.postSuggest.options) will come up as undefined even though I can see it when I run it without .options. This is also true for anything below .options.

I think it may be because .options is some sort of built-in function that acts on variables, so instead of seeing options as JSON and is instead trying to run a function on results.suggest.postSuggest

IanCZane
  • 600
  • 4
  • 21
  • 2
    `postSuggest` is an array (brackets, `[..]`) containing an object (braces, `{..}`). You'll need to access an index of the array first – `postSuggest[0].options`. – [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Jonathan Lonowski May 31 '16 at 04:23
  • Fixed my problem, thank you. I never registered that it was an array. – IanCZane May 31 '16 at 04:37

1 Answers1

1

arr.push(results.suggest.postSuggest.options)

postSuggest is an array of object.options inside postSuggest is also array of object. So first you need to get postSuggest by postSuggest[0] and then postSuggest[0].options to get array of options

This below snippet can be usefule

 var myObj = {..}
// used jquery just to demonstrate postSuggest  is an Array
console.log($.isArray(myObj.suggest.postSuggest)) //return true
var getPostSuggest =myObj.suggest.postSuggest  //Array of object
var getOptions = getPostSuggest[0].options; // 0 since it contain only one element
console.log(getOptions.length) ; // 5 , contain 5 objects
getOptions.forEach(function(item){
  document.write("<pre>Score is "+ item.score + " Text</pre>")
})

Jsfiddle

brk
  • 48,835
  • 10
  • 56
  • 78