Hasan Can Saral Hasan Can Saral - 6 months ago 116
JSON Question

Cannot Transform Correctly with Elasticsearch Watcher {{ctx.payload.hits.hits}}

I have a watcher configuration as follows:

{
"trigger": {
"schedule": {
"interval": "5s"
}
},
"input" : {
"search" : {
"request" : {
"indices" : [ "my_index" ],
"types" : [ "my_type" ],
"body" : {
"query" : {
"match_all" : {}
}
}
}
}
},
"transform" : {
"script" : "return [ body: groovy.json.JsonOutput.toJson(ctx.payload.hits.hits)]"
},
"actions" : {
"hbase_webhook" : {
"webhook" : {
"method" : "POST",
"host" : "<some_ip>",
"port" : <some_port>,
"path": "/v0.1/_events",
"body" : "data: {{ctx.payload.body}}"
}
}
}
}


The data posted in the body is not a valid JSON: Something like:

{ 'data: ': { '{"_index":"my_index","_type":"my_type","_source":{"key":"val"}},"_id":"<some_id>","_score":1.0}': '' } }


I don't know how to parse this output as JSON.parse in Node.js won't correctly parse it anyway.

Answer

Never. Forget. Headers.

I was forgetting:

"headers" {
    "Content-type": "application/json"
}

So it was impossible to parse with any tool.

Comments