0

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: enter image description here

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.

enter image description here

OJB1
  • 2,245
  • 5
  • 31
  • 63
  • JSON has a fixed set of rules. `queryString` does not fulfill these rules, hence it is not valid -> _"...an exception saying an unexpected token"_ – Andreas Aug 27 '21 at 17:26
  • `must` is an array of objects and not an array of invalid JSON -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Aug 27 '21 at 17:27
  • 1
    [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask): _"**DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text."_ – Andreas Aug 27 '21 at 17:35

2 Answers2

0

It looks like you could just add square brackets around the querystring to make json.parse work. Try:

var queryString = '{ "match": { "log.level": "Information" } }, { "match": { "metadata.log_event_category": "Open Id Connect" } }'

const must = JSON.parse(“[“ + queryString + ”]”);
must.push({
  "range": {
    "@timestamp": {
      "gte": chartArray[0].dateTimeFrom,
      "lte": chartArray[0].dateTimeTo
    }
  }
});


var request =
{
    "size": 0,
    "query": {
        "bool": {
            "must": must
        }
    },
    "aggs": { //etc }
};
James
  • 20,957
  • 5
  • 26
  • 41
  • Hi James, thanks for your reply, I'm finding that the double quotes shown in JSON.parse(“[“ + queryString + ”]”); is giving me squiggly line message "(JS) invalid Character" – OJB1 Aug 27 '21 at 17:46
  • Tried your example, unfortunately I get the the same issue where it tries to parse the queryString. Error message says "VM1344:1 Uncaught SyntaxError: Unexpected token at JSON.parse ()" What I can't figure out is when I manually write the match queries into the main request, I don't have the issues, its when try to insert them as a variable. thx – OJB1 Aug 27 '21 at 17:54
0

I had to split the match queries into separate items, then push them individually into the JSON request.

var must = [];

must.push(JSON.parse('{ "match": { "log.level": "Information" } }'))
must.push(JSON.parse('{ "match": { "metadata.log_event_category": "Open Id Connect" } }'))
must.push({
    "range": {
        "@timestamp": {
            "gte": chartArray[0].dateTimeFrom,
            "lte": chartArray[0].dateTimeTo
        }
    }
});

var request =
{
    "size": 0,
    "query": {
        "bool": {
            "must": must
        }
    },
    "aggs": {
        "myAggName": {
            "date_histogram": {
                "field": "@timestamp",
                [elasticIntervalType]: elasticIntervalUnits,
                "format": chartArray[0].scaleLabelAttributes
            }
        },
    }
}
OJB1
  • 2,245
  • 5
  • 31
  • 63