# How to merge JSON documents when ingesting?

**URL:** <https://discuss.elastic.co/t/how-to-merge-json-documents-when-ingesting/105096>\
**Category:** Logstash\
**Created:** [October 24, 2017, 4:13pm UTC](https://discuss.elastic.co/t/how-to-merge-json-documents-when-ingesting/105096 "2017-10-24T16:13:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinphillips](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@colinphillips](https://discuss.elastic.co/u/colinphillips)\
**Post date:** [October 24, 2017, 4:13pm UTC](https://discuss.elastic.co/t/how-to-merge-json-documents-when-ingesting/105096/1 "2017-10-24T16:13:37Z")

</div>

I'm new to logstash / beats and I'm wondering how best to go about transforming / merging two JSON documents. Here's an example of what I'm trying to do:

Document 1:

{  
"id": 1234,  
"things": [  
{  
"id": "1234",  
"lat": 45.12344,  
"long": 0.333232  
},  
{  
"id": "1234",  
"lat": 45.3322,  
"long": 0.456543  
}  
]  
}

Document 2:

{  
"id": 1234,  
"newField": 4321  
}

What I want to go into elasticsearch:

{  
"id": 1234,  
"newField": 4321,  
"things": [  
{  
"lat": 45.12344,  
"long": 0.333232  
},  
{  
"lat": 45.3322,  
"long": 0.456543  
}  
]  
}

So ... I have two documents at source (a REST API) that I want to merge based on the "id" field matching. I want to end up with a single document that contains elements of both source documents. In addition I am eliminating duplicate (and redundant) "id" fields in the first document.

Question:  
Architecturally, how is the best way to go about this? Should I pre-process the documents before they hit logstash (maybe with a custom beat?), or can I handle this case via the aggregate and json plugins directly in logstash? Should I build a custom (specialized) plugin for logstash?

---

<div class="post-metadata">

**Author:** ![Jack\_Judge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_judge/32/85499_2.png) [@Jack\_Judge](https://discuss.elastic.co/u/Jack_Judge)\
**Post date:** [October 24, 2017, 5:58pm UTC](https://discuss.elastic.co/t/how-to-merge-json-documents-when-ingesting/105096/2 "2017-10-24T17:58:50Z")

</div>

In the Elasticsearch output plugin there's a couple of options that'll help you, the "upsert" option and "document\_id".  
You're using the "id" field as the basis for merging. So use that field as the "document\_id" for logstash.

Post your updates to Elasticsearch as upserts and if a document doesn't exist with the id that you're using then one will be created. If a document with that id does exist then the upsert will update it. You can choose which fields to post so you can overwrite extraneous data.

---

<div class="post-metadata">

**Author:** ![colinphillips](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@colinphillips](https://discuss.elastic.co/u/colinphillips)\
**Post date:** [October 25, 2017, 6:33pm UTC](https://discuss.elastic.co/t/how-to-merge-json-documents-when-ingesting/105096/3 "2017-10-25T18:33:12Z")

</div>

Thanks Jack\_judge - I can see how to make that work, but I'm probably going to go another way as my API is OAUTH2 authenticated and I can't see a way to make that work directly with logstash. I'm planning to write a node.js app to authenticate with the API, and since I'm doing that the JSON transformation / aggregation that I'm after will be trivial in javascript.

---

<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:** [November 22, 2017, 6:33pm UTC](https://discuss.elastic.co/t/how-to-merge-json-documents-when-ingesting/105096/4 "2017-11-22T18:33:21Z")

</div>

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