# How to create index using json file?

**URL:** <https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java\
**Created:** [October 5, 2022, 9:00am UTC](https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137 "2022-10-05T09:00:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhinav-nath](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/abhinav-nath/32/2525_2.png) [@abhinav-nath](https://forum.opensearch.org/u/abhinav-nath)\
**Post date:** [October 5, 2022, 9:00am UTC](https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137/1 "2022-10-05T09:00:45Z")

</div>

Hi,

How can I create an index using a json file ?

Below code is not creating mappings for me:

```java
import jakarta.json.stream.JsonParser;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;

  public void createIndex() {
    try {
      JsonpMapper mapper = openSearchClient._transport().jsonpMapper();
      JsonParser parser = mapper.jsonProvider()
                                .createParser(new StringReader(Files.readString(Path.of(Objects.requireNonNull(getClass()
                                                                                                 .getClassLoader()
                                                                                                 .getResource(openSearchProperties.getIndexSource()))
                                                                                               .getPath()))));

      CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(openSearchProperties.getIndexName())
                                                                              .mappings(TypeMapping._DESERIALIZER.deserialize(parser, mapper))
                                                                              .build();
      CreateIndexResponse createIndexResponse = openSearchClient.indices().create(createIndexRequest);
      if (FALSE.equals(createIndexResponse.acknowledged())) {
        throw new RuntimeException("Create index response is not acknowledged");
      }
    } catch (Exception ex) {
      throw new RuntimeException("Failed to create index");
    }
  }

```

---

<div class="post-metadata">

**Author:** ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)\
**Post date:** [October 5, 2022, 9:43pm UTC](https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137/2 "2022-10-05T21:43:09Z")

</div>

Can you share more about what the inputs look like and what errors you are getting? Also, what versions of client and server you are using.

---

<div class="post-metadata">

**Author:** ![gkSachin04](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/gksachin04/32/4857_2.png) [@gkSachin04](https://forum.opensearch.org/u/gkSachin04)\
**Post date:** [February 14, 2023, 8:40am UTC](https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137/3 "2023-02-14T08:40:16Z")

</div>

> [@abhinav-nath](#):
>
> `TypeMapping._DESERIALIZER.deserialize(parser, mapper)`

InputStream inputStream = getClass().getClassLoader().getResourceAsStream(“mapping.json”);  
JsonpMapper mapper = client.\_transport().jsonpMapper();  
JsonParser parser = mapper.jsonProvider().createParser(inputStream);  
// CreateIndexRequest.Builder().index(index).mappings(TypeMapping.\_DESERIALIZER.deserialize(parser, mapper))  
client.indices().create(createIndexRequest → createIndexRequest.index(index)  
.mappings(TypeMapping.\_DESERIALIZER.deserialize(parser, mapper)));

This worked for me

---

<div class="post-metadata">

**Author:** ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)\
**Post date:** [February 14, 2023, 6:59pm UTC](https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137/4 "2023-02-14T18:59:49Z")

</div>

Awesome! Would you be up for offering sample code for our docs?

---

<div class="post-metadata">

**Author:** ![Murk](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Murk](https://forum.opensearch.org/u/Murk)\
**Post date:** [November 22, 2023, 2:29pm UTC](https://forum.opensearch.org/t/how-to-create-index-using-json-file/11137/5 "2023-11-22T14:29:32Z")

</div>

Hi, I am struggling with same problem. The index is created but with empty mappings. Is there any sample working code?
