# Defining a mapping for a stream of data with a random key

**URL:** <https://discuss.elastic.co/t/defining-a-mapping-for-a-stream-of-data-with-a-random-key/177701>\
**Category:** Elasticsearch\
**Created:** [April 20, 2019, 9:26am UTC](https://discuss.elastic.co/t/defining-a-mapping-for-a-stream-of-data-with-a-random-key/177701 "2019-04-20T09:26:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FluffySupreme](https://avatars.discourse-cdn.com/v4/letter/f/e19adc/32.png) [@FluffySupreme](https://discuss.elastic.co/u/FluffySupreme)\
**Post date:** [April 20, 2019, 9:26am UTC](https://discuss.elastic.co/t/defining-a-mapping-for-a-stream-of-data-with-a-random-key/177701/1 "2019-04-20T09:26:37Z")

</div>

I have data that looks like this:

```auto
{
  "somerandomkey" : {
    "field1" : "hello",
    "field2" : 123
  },
  "anotherrandomkey" : {
    "field1" : "world",
    "field2" : 321
  }
}

```

The keys before each object are not relevant to me. Is there a way to define a mapping so that elasticsearch either:

- ignores the key and treats the values as a stream?
- or, alternatively, creates a field from the key, adding it to the object?

The latter variant would transform this:

```auto
"somerandomkey" : {
  "field1" : "hello",
  "field2" : 123
}

```

into this:

```auto
{
  "field1" : "hello",
  "field2" : 123,
  "key" : "somerandomkey"
}

```

Using `jq`, I can do the former by applying `jq -ncr --stream 'fromstream(1|truncate_stream(inputs))` input.json` yielding:

```auto
{
  "field1" : "hello",
  "field2" : 123
},
{
  "field1" : "world",
  "field2" : 321
}

```

An option to get the data into the latter form is `jq 'with_entires(.value += {key}) | .[]'`, yielding:

```auto
{
  "field1" : "hello",
  "field2" : 123
  "key" : "somerandomkey"
}

```

The question is: do I need `jq` for that, or can elasticsearch do that for me?

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [April 21, 2019, 11:45am UTC](https://discuss.elastic.co/t/defining-a-mapping-for-a-stream-of-data-with-a-random-key/177701/2 "2019-04-21T11:45:26Z")

</div>

Hi,

Check about ingest, it can help you to reformat your data:  
[https://www.elastic.co/guide/en/elasticsearch/reference/7.0/ingest.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/ingest.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:** [May 19, 2019, 11:45am UTC](https://discuss.elastic.co/t/defining-a-mapping-for-a-stream-of-data-with-a-random-key/177701/3 "2019-05-19T11:45:29Z")

</div>

Closed on May 19, 2019, 11:45am UTC

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