Skip to content

How to traverse a complex JSON doc with javascript and extract named values

Andrey Gershun edited this page May 25, 2015 · 1 revision

How to traverse a complex JSON doc with javascript and extract named values?

Source: http://stackoverflow.com/questions/29966520/how-do-i-traverse-a-complex-json-doc-with-javascript-and-extract-named-values

Question

It has a specific problem to solve, extracting a named value from Json, I need some javascript to traverse reasonably complex json with nested objects and arrays, and extract values. Example json is
  var data = {
  "query": {
      "filtered": {
          "query": {
              "match_all": {}
          },
          "filter": {
              "and": {
                  "filters": [
                      {
                          "terms": {
                              "ACCOUNT_NUMBER": [
                                  "37846589",
                                  "37846540"
                              ]
                          }
                      }
                  ]
              }
          }
      }
    }
  };

Answer

You can do it with AlaSQL SEARCH operator:

    var res = alasql('SEARCH /+ACCOUNT_NUMBER/ FROM ?', [data]);
Clone this wiki locally