【问题标题】:How to add a string variable to a JSON query in Javascript without adding extra escaping如何在 Javascript 中将字符串变量添加到 JSON 查询而不添加额外的转义
【发布时间】:2021-08-27 17:21:53
【问题描述】:

我无法将字符串变量添加到 JSON 的一部分,当我将完整的 JSON 请求传递到服务器时,我添加到 JSON 的字符串变量总是包含额外的转义引号和我不知道如何在插入时删除它们。

我已尝试使用 JSON.Parse(queryString),但随后出现异常,提示意外令牌。

我尝试添加的匹配查询的数量会因应用的报告过滤器的数量而异,因此我无法在 JSON 代码块中对匹配查询进行硬编码,因为它们显示在注释掉的部分.

代码示例:

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
            }
        },
    }
}

屏幕截图显示,在发送到服务器时,我从匹配查询中获得了额外的转义引号,但我需要删除这些:

下面是一个工作示例的屏幕截图,但我正在尝试在 javascript 中复制构建这个等效请求,以便传递给我的控制器并发送到 Elastic Search。

【问题讨论】:

  • JSON 有一套固定的规则。 queryString 不符合这些规则,因此它无效 -> "...一个异常说一个意外的令牌"
  • must 是一个对象数组,而不是一个无效 JSON 数组 -> What is the difference between JSON and Object Literal Notation?
  • How do I ask a good question?: "请勿发布代码、数据、错误消息等的图片 - 复制或在问题中输入文本。请保留将图像用于图表或演示渲染错误,这些无法通过文本准确描述的事情。”

标签: javascript json


【解决方案1】:

看起来你可以在查询字符串周围添加方括号来使 json.parse 工作。试试:

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 }
};

【讨论】:

  • 嗨,詹姆斯,感谢您的回复,我发现 JSON.parse(“[“ + queryString + ”]”); 中显示的双引号正在给我波浪线消息“(JS)无效字符”
  • 试过你的例子,不幸的是我遇到了同样的问题,它试图解析查询字符串。错误消息显示“VM1344:1 Uncaught SyntaxError: Unexpected token at JSON.parse ()”我无法弄清楚的是,当我手动将匹配查询写入主请求时,我没有问题,当尝试将它们作为变量插入时。谢谢
【解决方案2】:

我必须将匹配查询拆分为单独的项目,然后将它们单独推送到 JSON 请求中。

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
            }
        },
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2023-03-12
    相关资源
    最近更新 更多