# Indexing JSON with nested fields

**URL:** <https://discuss.elastic.co/t/indexing-json-with-nested-fields/161941>\
**Category:** Elasticsearch\
**Tags:** es-hadoop\
**Created:** [December 23, 2018, 7:29am UTC](https://discuss.elastic.co/t/indexing-json-with-nested-fields/161941 "2018-12-23T07:29:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Buntu\_Dev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buntu_dev/32/117258_2.png) [@Buntu\_Dev](https://discuss.elastic.co/u/Buntu_Dev)\
**Post date:** [December 23, 2018, 7:29am UTC](https://discuss.elastic.co/t/indexing-json-with-nested-fields/161941/1 "2018-12-23T07:29:07Z")

</div>

I've a nested json data with nested fields that I want to extract and construct a Scala Map.

Heres the sample JSON:

```
"nested_field": [
  {
    "airport": "sfo",
    "score": 1.0
  },
  {
    "airport": "phx",
    "score": 1.0
  },
  {
    "airport": "sjc",
    "score": 1.0
  }
]

```

I want to use saveToES() and construct a Scala Map to index the field into ES index with mapping as below:

```
 "nested_field": {
    "properties": {
      "score": {
        "type": "double"
      },
      "airport": {
        "type": "keyword",
        "ignore_above": 1024
      }
    }
  }

```

The json file is read into the dataframe using spark.read.json("example.json"). Whats the right way to construct the Scala Map in this case?

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![james.baiera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/james.baiera/32/10209_2.png) [@james.baiera](https://discuss.elastic.co/u/james.baiera)\
**Post date:** [January 2, 2019, 4:38pm UTC](https://discuss.elastic.co/t/indexing-json-with-nested-fields/161941/2 "2019-01-02T16:38:55Z")

</div>

> [@Buntu\_Dev](#):
>
> Whats the right way to construct the Scala Map in this case?

This seems more like a generic Spark question. If you're asking how to parse JSON using Scala, that can be done hundreds of different ways, all of which are reasonable solutions. That's generally not a helpful tip though so to give you a starting spot: ES-Hadoop uses the Jackson JSON libraries to parse JSON into objects and vice versa. I would peruse that library as a good place to start.

As for ensuring the mapping is what you want, I would suggest either creating the index before running your job with your desired mapping. If you don't want to precreate the index every time and would rather ES-Hadoop do that, you can create an index template in Elasticsearch that will assign the mapping you want to any index who's name matches its pattern.

---

<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:** [January 30, 2019, 4:47pm UTC](https://discuss.elastic.co/t/indexing-json-with-nested-fields/161941/3 "2019-01-30T16:47:53Z")

</div>

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