# How to handle JSON arrays so that they're searchable in Kibana?

**URL:** <https://discuss.elastic.co/t/how-to-handle-json-arrays-so-that-theyre-searchable-in-kibana/67003>\
**Category:** Logstash\
**Created:** [November 23, 2016, 3:19pm UTC](https://discuss.elastic.co/t/how-to-handle-json-arrays-so-that-theyre-searchable-in-kibana/67003 "2016-11-23T15:19:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadyabhi](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shadyabhi](https://discuss.elastic.co/u/shadyabhi)\
**Post date:** [November 23, 2016, 3:19pm UTC](https://discuss.elastic.co/t/how-to-handle-json-arrays-so-that-theyre-searchable-in-kibana/67003/1 "2016-11-23T15:19:44Z")

</div>

We’ve JSON documents that contain arrays.

```
# Doc 1
{
    "test": {
        "steps": [{
            "response_time": "100"
        }, {
            "response_time": "101"
        }]
    }
}

# Doc 2
{
    "test": {
        "steps": [{
            "response_time": "101"
        }, {
            "response_time": "100"
        }]
    }
}

```

Now, I want to search for documents which have response\_time=101 in the second element of test.steps.  
If I write a query like this:- (query\_string type is what Kibana uses)

```
{
    "query": {
        "query_string": {
           "query": "test.steps.response_time:101"
        }
    }
}

```

It will match both documents and there is no way to specify which element I should search on. This is because Elasticsearch flattens the json internally. I wish ES had this feature but I can’t write a query like test.steps[1].response\_time:101 and expect it to work.

A way to solve this is writing a filter in logstash that converts all arrays into flat documents while preserving the ordering. For ex,

Convert:

```
{
    "test": {
        "steps": [{
            "response_time": "100"
        }, {
            "response_time": "101"
        }]
    }
}

```

to this:

```
{
    "test": {
        "steps1": { "response_time": "100" },
        "steps2": { "response_time": "100" }
    }
}

```

This seems like a common problem that other people might have faced. Do we have a filter already built to handle this?

---

<div class="post-metadata">

**Author:** ![shadyabhi](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shadyabhi](https://discuss.elastic.co/u/shadyabhi)\
**Post date:** [November 25, 2016, 3:33pm UTC](https://discuss.elastic.co/t/how-to-handle-json-arrays-so-that-theyre-searchable-in-kibana/67003/2 "2016-11-25T15:33:02Z")

</div>

I came up with a solution myself. Slightly different than what I initially thought but it works better.

```
ruby {
    init => "
        def arrays_to_hash(h)
          h.each do |k,v|
            # If v is nil, an array is being iterated and the value is k.
            # If v is not nil, a hash is being iterated and the value is v.
            value = v || k
            if value.is_a?(Array)
                # "value" is replaced with "value_hash" later.
                value_hash = {}
                value.each_with_index do |v, i|
                    value_hash[i.to_s] = v
                end
                h[k] = value_hash
            end

            if value.is_a?(Hash) || value.is_a?(Array)
              arrays_to_hash(value)
            end
          end
        end
      "
      code => "arrays_to_hash(event.to_hash)"
}

```

More details: [http://blog.abhijeetr.com/2016/11/logstashelasticsearch-best-way-to.html](http://blog.abhijeetr.com/2016/11/logstashelasticsearch-best-way-to.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 23, 2016, 3:33pm UTC](https://discuss.elastic.co/t/how-to-handle-json-arrays-so-that-theyre-searchable-in-kibana/67003/3 "2016-12-23T15:33:06Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
