# Ingest JSON data into elasticsearch using logstash

**URL:** <https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558>\
**Category:** Logstash\
**Created:** [April 19, 2021, 9:28am UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558 "2021-04-19T09:28:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)\
**Post date:** [April 19, 2021, 9:28am UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558/1 "2021-04-19T09:28:19Z")

</div>

HI All,

I have a complicated json data to ingest into elasticsearch, need your suggestion to achieve this.  
Here is my data format,

```
<result_set elemtype="list">
<dataset>
<device>/api/device/13766</device>
<field_names elemtype="list">
<v>collection_time</v>
<v>d_check</v>
</field_names>
<data elemtype="list">
<data elemtype="list">
<v>1618644420</v>
<v>1</v>
</data>
</data>
</dataset>
<dataset>
<device>/api/device/13769</device>
<field_names elemtype="list">
<v>collection_time</v>
<v>d_check</v>
</field_names>
<data elemtype="list">
<data elemtype="list">
<v>1618644420</v>
<v>1</v>
</data>
</data>
</dataset>
<dataset>
<device>/api/device/13772</device>
<field_names elemtype="list">
<v>collection_time</v>
<v>d_check</v>
</field_names>
<data elemtype="list">
<data elemtype="list">
<v>1618644420</v>
<v>1</v>
</data>
</data>
</dataset>
<dataset>
<device>/api/device/13773</device>
<field_names elemtype="list">
<v>collection_time</v>
<v>d_check</v>
</field_names>
<data elemtype="list">
<data elemtype="list">
<v>1618644420</v>
<v>1</v>
</data>
</data>
</dataset>

```

Here for my device ID the data is proper ist coming like device : /api/device/13769

When it comes for he collection time and avilability the data is like fieldname itself coming as a field and the data in a separate field

```
<field_names elemtype="list">
<v>collection_time</v>
<v>d_check</v>
</field_names>
<data elemtype="list">
<data elemtype="list">
<v>1618644420</v>
<v>1</v>

```

How i want it to be indexed is

```
device : /api/device/13769
collection_time : 1618644420
d_check : 1

```

Please help to proceed with this.

Thanks  
Gautham

---

<div class="post-metadata">

**Author:** ![jasenj1](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@jasenj1](https://discuss.elastic.co/u/jasenj1)\
**Post date:** [April 19, 2021, 1:36pm UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558/2 "2021-04-19T13:36:53Z")

</div>

That's not JSON. That's XML.

---

<div class="post-metadata">

**Author:** ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)\
**Post date:** [April 19, 2021, 1:48pm UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558/3 "2021-04-19T13:48:12Z")

</div>

@jasenj1 Sorry, here is the json.

```
"result_set": [
        {
            "device": "/api/device/13766",
            "field_names": [
                "collection_time",
                "d_check"
            ],
            "data": [
                [
                    "1618645620",
                    "1"
                ]
            ]
        },
        {
            "device": "/api/device/13769",
            "field_names": [
                "collection_time",
                "d_check"
            ],
            "data": [
                [
                    "1618645620",
                    "1"
                ]
            ]
        },

```

Thanks  
Gautham

---

<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:** [April 19, 2021, 3:27pm UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558/4 "2021-04-19T15:27:12Z")

</div>

Any time the field names are variables you need to use ruby. For the JSON you show it would be

```
    ruby {
        code => '
            rs = event.get("result_set")
            newRs = []
            rs.each_index { |x|
                h = rs[x]
                h["data"][0].each_index { |y|
                    h[h["field_names"][y]] = h["data"][0][y]
                }
                h.delete("data")
                h.delete("field_names")
                newRs << h
            }
            event.set("result_set", newRs)
        '
    }
```

---

<div class="post-metadata">

**Author:** ![Gauti](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gauti](https://discuss.elastic.co/u/Gauti)\
**Post date:** [April 19, 2021, 6:33pm UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558/5 "2021-04-19T18:33:59Z")

</div>

Thank you @Badger It worked perfectly.

Thanks  
Gauti

---

<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:** [May 17, 2021, 6:34pm UTC](https://discuss.elastic.co/t/ingest-json-data-into-elasticsearch-using-logstash/270558/6 "2021-05-17T18:34:50Z")

</div>

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