# Converting a JSON array into object key pairs

**URL:** <https://discuss.elastic.co/t/converting-a-json-array-into-object-key-pairs/180547>\
**Category:** Logstash\
**Created:** [May 10, 2019, 11:43am UTC](https://discuss.elastic.co/t/converting-a-json-array-into-object-key-pairs/180547 "2019-05-10T11:43:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saiteja\_c](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saiteja_c/32/45793_2.png) [@saiteja\_c](https://discuss.elastic.co/u/saiteja_c)\
**Post date:** [May 10, 2019, 11:43am UTC](https://discuss.elastic.co/t/converting-a-json-array-into-object-key-pairs/180547/1 "2019-05-10T11:43:42Z")

</div>

What I'm trying to achieve is I have input like

```
 "jsonBody": {
         "measurements": [
           {
             "value": 0,
             "statistic": "VALUE"
           },
          {
             "value": 1,
             "statistic": "VALUE2"
           },
         ]
}

```

and I am trying to convert it into something like this

```
 "jsonBody": {

      "measurements"
        {
           "VALUE":0,
           "VALUE2":1
        }
}

```

So far i Have tried to do this

```
filter{
      ruby{
        code => 'b= event.get("[response][jsonBody][measurements]")
        
        
        Array(b).each{|kv| event.set(kv["statistic"], kv["value"])}'
          
      }

}

```

and

```
filter{
      ruby{
        code => 'b= event.get("[response][jsonBody][measurements]")
        
        
        b.each{|kv| event.set(kv["statistic"], kv["value"])}'
          
      }

}

```

Both of these methods don't seem to work very well.  
What am I missing?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 10, 2019, 2:31pm UTC](https://discuss.elastic.co/t/converting-a-json-array-into-object-key-pairs/180547/2 "2019-05-10T14:31:53Z")

</div>

Try

```
ruby {
    code => '
        b = event.get("[response][jsonBody][measurements]")
        h = {}
        b.each { |kv|
            h[kv["statistic"]] = kv["value"]
        }
        event.set("[response][jsonBody][measurements]", h)
    '
  }
```

---

<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:** [June 7, 2019, 2:32pm UTC](https://discuss.elastic.co/t/converting-a-json-array-into-object-key-pairs/180547/3 "2019-06-07T14:32:17Z")

</div>

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