# Painless scripting JSON functions

**URL:** <https://discuss.elastic.co/t/painless-scripting-json-functions/98511>\
**Category:** Elasticsearch\
**Created:** [August 28, 2017, 6:30am UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511 "2017-08-28T06:30:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HDB](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@HDB](https://discuss.elastic.co/u/HDB)\
**Post date:** [August 28, 2017, 6:30am UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/1 "2017-08-28T06:30:52Z")

</div>

In Painless scripting how can I do JSON.parse and JSON.stringify?

I have kept JSON data in the single string field, but now I want to delete some of the fields. So I will be needing JSON functions.

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [August 28, 2017, 11:42pm UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/2 "2017-08-28T23:42:52Z")

</div>

@HDB there are no JSON parsing classes in the [Painless API](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html).

To do what you're considering, I think you'll want to re-index that data using Logstash to convert the string field into a JSON object using the [JSON filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html) and [dropping fields](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html#plugins-filters-json-remove_field).

Logstash pipeline: [Elasticsearch input](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html#plugins-filters-json-remove_field) --\> [JSON filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html) --\> [Elasticsearch output](https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html)

---

<div class="post-metadata">

**Author:** ![HDB](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@HDB](https://discuss.elastic.co/u/HDB)\
**Post date:** [August 31, 2017, 5:08pm UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/3 "2017-08-31T17:08:27Z")

</div>

Thanks Mike,

I am planning to use elastic-js scripting, will that work fine? Or any performance issues that we can face?

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [August 31, 2017, 6:14pm UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/4 "2017-08-31T18:14:32Z")

</div>

Do you mean using a javascript client library (like [this](https://github.com/fullscale/elastic.js/) or [this](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html))? No, I don't think that will do what you want since there will not (AFAIK) be general JSON-manipulation methods within it. However, you would be able to use regular javascript.

So, you'd use the ES.js client to query the data, use regular javascript to manipulate it (drop fields, etc), then use the client to push the results back.

---

<div class="post-metadata">

**Author:** ![HDB](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@HDB](https://discuss.elastic.co/u/HDB)\
**Post date:** [August 31, 2017, 6:26pm UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/5 "2017-08-31T18:26:21Z")

</div>

I was pointing out for [https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#\_general\_purpose\_languages](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_general_purpose_languages). To update documents will execute the script placed under config/scripts written in the javascript.

And, Will need to enable the language support in elastic service.

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [August 31, 2017, 6:49pm UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/6 "2017-08-31T18:49:53Z")

</div>

OK, but as heads up, the only language you'll be able to use for that  
purpose in the upcoming v6.0 is our Painless scripting language. See  
[https://github.com/elastic/elasticsearch/issues/20129](https://github.com/elastic/elasticsearch/issues/20129) and  
[https://www.elastic.co/guide/en/elasticsearch/reference/6.0/modules-scripting.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/modules-scripting.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:** [September 28, 2017, 6:50pm UTC](https://discuss.elastic.co/t/painless-scripting-json-functions/98511/7 "2017-09-28T18:50:14Z")

</div>

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