# How to extract string from JSON string using painless script

**URL:** <https://discuss.elastic.co/t/how-to-extract-string-from-json-string-using-painless-script/262513>\
**Category:** Elasticsearch\
**Created:** [January 28, 2021, 2:20pm UTC](https://discuss.elastic.co/t/how-to-extract-string-from-json-string-using-painless-script/262513 "2021-01-28T14:20:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aniket\_Salvi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aniket_salvi/32/45433_2.png) [@Aniket\_Salvi](https://discuss.elastic.co/u/Aniket_Salvi)\
**Post date:** [January 28, 2021, 2:20pm UTC](https://discuss.elastic.co/t/how-to-extract-string-from-json-string-using-painless-script/262513/1 "2021-01-28T14:20:35Z")

</div>

Hi team,

script:

```
if(doc['charstream.keyword'].size() == 0){
     return "null"
} else{
    String[] parts = /\|/.split(doc['charstream.keyword'].value);
    return parts[4];
}    

```

Message string:

```
"charstream": "Content-Type: application/json\r\nIXC-Client-API-Type: REST-XML/HTTP\r\n\r\n{\"api_version\":\"1.2.0\",\"response_id\":\"BEFC9B92-F8C7-11EA-96EA-7F0001010000\",\"response_datetime\":\"2020-09-17T14:55:37.462+05:30\",\"request_id\":\"CD758BDD-C0A4-48AC-AF5D-BBD89646D196\",\"request_datetime\":\"2020-09-17T14:55:37.404+05:30\",\"sender_system_id\":\"Profunds\",\"sender_system_user_id\":\"abc@xyz.com\",\"request_priority\":\"3\",\"test_indicator\":\"false\",\"client_code\":\"ACMEX\",\"virtual_account_number\":\" ****\",\"transaction_amount\":\"**** \",\"currency_code\":\"INR\",\"payment_mode\":\" ****\",\"transaction_id\":\"**** \",\"transaction_id_type\":\"UTR\",\"transaction_datetime\":\"2020-09-17T14:50:27.404\",\"sender_name\":\" ****\",\"sender_mobile_communication_number\":\"**** \",\"sender_account_number\":\" ****\",\"sender_bank_name\":\"**** \",\"sender_ifsc_code\":\" ****\",\"client_account_name\":\"**** \",\"client_mobile_communication_number\":\" ****\",\"client_account_number\":\"**** \",\"client_bank_name\":\" ****\",\"client_ifsc_code\":\"**** \",\"narration_1\":\"Narration 1\",\"narration_2\":\"Narration 2\",\"informational_message\":\"A quick brown fox jumped over the lazy dog !\",\"completion_code\":0,\"reason_code\":\"XC00441I\",\"message\":\"Client has confirmed this transaction to be a valid one.\",\"client_data_ext\":{\"response_id\":\"727C43C3-8A4B-45CD-ABF2-5FFF564A9C77\",\"response_datetime\":\"2020-09-17T09:25:37.447Z\"}}"

```

output is:

```
[
{
  "_id": "6doI03YBCWsN2ibkTlAi",
  "amount": [
   "null"
  ]
 },
 {
  "_id": "ebH-HncBYnsBpTq8Oub_",
  "amount": [
   "null"
  ]
 }
]

```

I need following fields from above JSON string:

1. transaction\_amount
2. sender\_name
3. virtual\_account\_number
4. sender\_account\_number

Can anyone has solution ?

Thanks

---

<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:** [February 25, 2021, 2:20pm UTC](https://discuss.elastic.co/t/how-to-extract-string-from-json-string-using-painless-script/262513/2 "2021-02-25T14:20:44Z")

</div>

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