Converting Json String with Processor — Elasticsearch

André Coelho
2 min readJan 27, 2023

Today we are going to use Json Processor to convert json string into json objects.

I will use two structures for the tests, the first will be a json string with an array.

json = [{\”color\”:\”red\”,\”value\”:\”#f00\”},{\”color\”:\”green\”,\”value\”:\ “#0f0\”}]

POST /_ingest/pipeline/_simulate?verbose=true
{
"pipeline": {
"description": "convert json string to document",
"processors": [
{
"json": {
"field": "json_string",
"target_field": "colors"
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"json_string": "[{\"color\":\"red\",\"value\":\"#f00\"},{\"color\":\"green\",\"value\":\"#0f0\"}]"
}
}
]
}

I want the colors field to become the array with each json item.

Output:

"doc": {
"_index": "index",
"_id": "id",
"_version": "-3",
"_source": {
"json_string": """[{"color":"red","value":"#f00"},{"color":"green","value":"#0f0"}]""",
"colors": [
{
"color": "red",
"value": "#f00"
},
{
"color": "green",
"value": "#0f0"
}
]
}

Now we will have another json: {\”employee\”:{\”name\”:\”sonoo\”,\”salary\”:56000,\”married\”:true}}.

We are going to make a change which is to add the “add_to_root” parameter, we want this “employee” object to be at the top level of the document. However, when using this parameter, we cannot define the “target_field”.

POST /_ingest/pipeline/_simulate?verbose=true
{
"pipeline": {
"description": "convert json string to document",
"processors": [
{
"json": {
"add_to_root": true,
"field": "json_string"
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"json_string": "{\"employee\":{\"name\":\"john doo\",\"salary\":1500,\"married\":false}}"
}
}
]
}

Output

          "doc": {
"_index": "index",
"_id": "id",
"_version": "-3",
"_source": {
"employee": {
"name": "john doo",
"salary": 1500,
"married": false
},
"json_string": """{"employee":{"name":"john doo","salary":1500,"married":false}}"""
},
"_ingest": {
"pipeline": "_simulate_pipeline",
"timestamp": "2023-01-27T04:12:05.8328892Z"
}
}

In both cases we were able to convert the json string. For more details on Json Processor parameters visit the documentation.

References

--

--

André Coelho

Developer of web and mobile systems. Enthusiast in the area of ​​automation and electronics and I have hobbie music.