I'm having an issues in being able to add a string variable to a section of JSON, and when I pass the full JSON request to the server, the string variable that I added to the JSON always contains extra escape quotes and I don't know how to remove them on insertion.
I've tried using JSON.Parse(queryString) but I then get an exception saying an unexpected token.
The match queries I'm trying to add will vary in number depending on the qty of report filters applied, therefore I can't hard code the match queries in the JSON code block as they're shown in the commented out section.
Code sample:
var queryString = '{ "match": { "log.level": "Information" } }, { "match": { "metadata.log_event_category": "Open Id Connect" } }'
var request =
{
"size": 0,
"query": {
"bool": {
"must": [
// Need to insert a string variable here that would contain the match queries commented out below...
// queryString, // doesnt work, adding the var here results in extra escapes
// JSON.parse(queryString), doesnt work!
// If I manually write in the match queries below then everything goes through OK
//{ "match": { "log.level": "Information" } },
//{ "match": { "metadata.log_event_category": "Open Id Connect" } },
{
"range": {
"@timestamp": {
"gte": chartArray[0].dateTimeFrom,
"lte": chartArray[0].dateTimeTo
}
}
}
]
}
},
"aggs": {
"myAggName": {
"date_histogram": {
"field": "@timestamp",
[elasticIntervalType]: elasticIntervalUnits,
"format": chartArray[0].scaleLabelAttributes
}
},
}
}
Screenshot shows that I'm getting extra escape quotes from the match queries when sending to the server, but I need these to be removed:
Below is a screenshot of a working example but I'm trying to replicate building this equivalent request in javascript in order to pass to my controller and send off to Elastic Search.