javascript - 使用 jsonpath 提取 json 中的元素值

标签 javascript jmeter jsonpath json-path-expression

以下是我的json数据

{
    "primary": [
        {
            "secondary": {
                "name": {
                    "fullname": "fullname1"
                },
                "alias": "alias1"
            },
            "reference": "reference1"
        },
        {
            "secondary": {
                "name": {
                    "fullname": "fullname2"
                },
                "alias": "alias2"
            },
            "reference": "reference2"
        }
    ]
}

现在我想使用 jsonpath 从此 json 数据中根据基于条件的全名值提取“别名”值。

我正在使用以下表达式,但无法解析结果

$.primary[*].secondary[?(@.name.fullname == "fullname1")].alias

最佳答案

您的 JSON 格式错误,如果它确实像您发布的那样 - 您将无法使用 JSON Extractor,您必须切换到 Regular Expression Extractor

有效的 JSON 类似于:

{
  "primary": [
    {
      "secondary": {
        "name": {
          "fullname": "fullname1"
        },
        "alias": "alias1"
      },
      "reference": "reference1"
    },
    {
      "secondary": {
        "name": {
          "fullname": "fullname2"
        },
        "alias": "alias2"
      },
      "reference": "reference2"
    },
    {
      "type": "online"
    }
  ]
}

如果它是正确的,你的JsonPath表达式工作正常,如 JsonPath Tester mode of the View Results Tree listener 所证明的那样:

enter image description here

关于javascript - 使用 jsonpath 提取 json 中的元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58707788/

相关文章:

javascript - Chrome 不再允许输入类型 ="number"上的 selectionStart/selectionEnd

javascript - HTML5 拖放图像在 Chromium(和 Opera)中获得水平偏移

java - JMeter Post 请求 - JSON 解析错误 : Invalid UTF-8 start byte 0xb0

java - Spring MVC 测试中带有过滤器的 JsonPath 表达式

c# - 在 Newtonsoft.Json.Linq.JObject 上运行表达式谓词

javascript - Highcharts 每个系列列一个值

javascript - 立即检测正在检查的输入

jmeter - JMeter无法登录网站-JMeter Noob

testing - 如何加载测试纯java代码?

javascript - 修改一个复杂的深度嵌套的 javascript 对象