标签: jsonpath

使用wiremock在JsonPath中匹配数据

我正在尝试为我的登录程序创建模拟.我使用POST方法与几个字段和登录对象(使用登录名,密码等)为此我使用JsonPath.代码如下:

{
"request": {
        "method": "POST",
        "url": "/login",
        "bodyPatterns" : [
                {"matchesJsonPath" : "$.method"},
                {"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
                {"matchesJsonPath" : "$.params.login"},
                {"matchesJsonPath" : "$.params.password"}
         ]
    },
    "response": {
            "status": 200,
            "bodyFileName": "login.json"
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在检查clientVersion,因为它与示例类似.

我的问题是,用给定的POST JSON:

{
    "method": "login",
    "params": {
        "clientVersion": "1",
        "login": "test@test.com",
        "password": "681819535da188b6ef2"
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了404.但是,当我改变时

{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
Run Code Online (Sandbox Code Playgroud)

正常

{"matchesJsonPath" : "$.params.clientVersion"},
Run Code Online (Sandbox Code Playgroud)

一切正常.

那么 - 如果给定的字段等于某个值,如何使用matchesJsonPath检查内部的wiremock?在我的情况下,如何将它应用于根域,就像方法一样?虽然我们在这里 - 我在检查值是否为空时遇到了类似的问题.我试图应用正则表达式等 - 没有运气.

json jsonpath wiremock

8
推荐指数
2
解决办法
1万
查看次数

在Python中,使用jsonpath-rw获取特定属性的值(json/dict)

这是我的json:

{
   'test': [
        { "id": "1", "description": "Test 1" },
        { "id": "2", "description": "Test 2" }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取id的值,其中描述为"Test 1".

我在JsonPath页面上找到了以下示例:

$..book[?(@.price<10)]
Run Code Online (Sandbox Code Playgroud)

尝试解析以下jsonxpath表达式时:

parse('$..test[?(@.description="Test 1")].id')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

jsonpath_rw.lexer.JsonPathLexerError: Error on line 1, col 7: Unexpected character: ?
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?或者,有更好的方法吗?

python jsonpath

8
推荐指数
1
解决办法
5167
查看次数

如果 k8s 的 jsonpath 缺少 AND 条件,如何执行 2 个过滤器

我需要找到所有副本仍在启动的 st。为此,我需要根据 2 个条件过滤 sts 项目:

  • 每个项目必须有一个.status.readyReplicas属性
  • 每个readyReplicas属性必须与中的值不同replicas

我尝试的结果出现错误:

$ kubectl get sts -o=jsonpath='{range .items[?(@.status.readyReplicas && @.status.readyReplicas!=@.status.replicas)]}{.}{.metadata.name}{"\t"}{.status.readyReplicas}{"/"}{.status.replicas}{"\n"}{end}'
error: error parsing jsonpath {range .items[?(@.status.readyReplicas && @.status.readyReplicas!=@.status.replicas)]}{.}{.metadata.name}{"\t"}{.status.readyReplicas}{"/"}{.status.replicas}{"\n"}{end}, unrecognized character in action: U+0026 '&'

$ kubectl get sts -o=jsonpath='{range .items[?(@.status.readyReplicas AND @.status.readyReplicas!=@.status.replicas)]}{.}{.metadata.name}{"\t"}{.status.readyReplicas}{"/"}{.status.replicas}{"\n"}{end}'
error: error executing jsonpath "{range .items[?(@.status.readyReplicas AND @.status.readyReplicas!=@.status.replicas)]}{.}{.metadata.name}{\"\\t\"}{.status.readyReplicas}{\"/\"}{.status.replicas}{\"\\n\"}{end}": Error executing template: unrecognized identifier AND. Printing more information for debugging the template: ...
Run Code Online (Sandbox Code Playgroud)

使用逗号在数组上的 JsonPath AND 运算符上建议的解决方案也不起作用:

$ k get sts -o=jsonpath='{range .items[?(@.status.readyReplicas), ?(@.status.readyReplicas!=@.status.replicas)]}{.}{.metadata.name}{"\t"}{.status.readyReplicas}{"/"}{.status.replicas}{"\n"}{end}'
error: error …
Run Code Online (Sandbox Code Playgroud)

jsonpath kubernetes

8
推荐指数
0
解决办法
1054
查看次数

有没有办法在 JSONPath 中获取字符串值的子字符串?

基本上,我需要使用 JSONPath 获取字符串值的一部分。我无法先使用 JSONPath 获取完整值,然后使用另一种语言获取它的一部分。

有没有办法在 JSONPath 中做到这一点?

json jsonp jsonpath

7
推荐指数
1
解决办法
4081
查看次数

使用 MockMVC 的 JsonPath OR 条件

我正在练习 MockMVC 进行休息呼叫单元测试。我们如何测试布尔值以便结果是 true 还是 false 我需要通过测试,我尝试如下,

mockMvc.perform(get("/student/{Id}", 1L)).
.andExpect(status().isOk())
.andExpect(jsonPath("$.isPass", is(true  || false)));
Run Code Online (Sandbox Code Playgroud)

我还有包含 6 个值的列表,如何使用包含各种方法的列表,

.andExpect(jsonPath("$.subjectList", hasSize(5)))
.andExpect(jsonPath("$.subjectList.name", Matchers.contains("English", "Hindi", "France", "Tamil", "Bengali"))
Run Code Online (Sandbox Code Playgroud)

有什么建议请!!

junit spring jsonpath mockmvc spring-rest

7
推荐指数
1
解决办法
5341
查看次数

JSONPath 通过子子值获取父元素的 id

给定以下 JSON,我想id通过子子元素的等于文本比较来获取父项的字段:

{
    "datapoints": [{
            "id": "default.1",
            "definedBy": "default/0.1",
            "featureValues": {
                "bui.displayname": "Health status",
                "bui.visibility": "normal",
                "default.access": "r",
                "default.basetype": "text",
                "default.description": "Aggregated health status",
                "default.format": "text/plain",
                "default.name": "health_status",
                "default.restriction": "re:(OK|WARN|ERROR|UNKNOWN)"
            }
        }, {
            "id": "kdl.240",
            "definedBy": "kdl/0.9",
            "featureValues": {
                "bui.displayname": "Delta K",
                "bui.visibility": "normal",
                "default.access": "rw",
                "default.basetype": "real",
                "default.description": "Delta K",
                "default.name": "Delta_K",
                "default.privacy": "false",
                "default.restriction": "b32"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的第一个目标是通过子子文本比较来获得正确的数据点,例如:

$['datapoints'][*]['featureValues'][?(@['default.name']=='Delta_K')]

当我在http://jsonpath.com/上测试时它似乎不起作用 为了获得我成功使用的所有数据点:

$['datapoints'][*]['featureValues']['default.name']

我的目标是获得idfeatureValues子元素default.name相等的数据点的值Delta_K。在这个例子中,这将是kdl.240 …

json jsonpath

7
推荐指数
2
解决办法
7527
查看次数

JsonPath - 如何从不同级别读取属性

我尝试使用 JsonPath 从 json 对象不同级别的属性中提取值。

这是我的对象:

{
    "A": [
        {
            "B": "0",
            "C": "1",
            "D": {
                "E" : "2",
                "F" : "3",
            }
        },
        {
            "B": "4",
            "C": "5",
            "D": {
                "E" : "6",
                "F" : "7",
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想同时提取 B 和 E 值

获得 B,很简单:$.A[*].B

得到E,很简单:$.A[*].DE

要获得 B 和 C,可以轻松使用括号标记的子运算符: $.A[*]['B', 'C']

但要同时得到 B 和 E,我没有得到正确的表达式。我期望有:

[ 
  { "B":"0", "E" : "2"}, 
  { "B":"4", "E" : "7"} 
]
Run Code Online (Sandbox Code Playgroud)

有人有正确的表达吗?

谢谢

E.

java jsonpath

7
推荐指数
0
解决办法
746
查看次数

获取最近创建的 pod 的名称

在 OpenShift 中,有没有比这个更优雅的方法来获取应用程序中最近创建的 pod 的名称my_app

name=$(oc get pods -l app=my_app -o=jsonpath='{range.items[*]}{.status.startTime}{"\t"}{.metadata.name}{"\n"}{end}' | sort -r | head -1 | awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)

这个想法是排序.status.startTime并输出一个.metadata.name。到目前为止,我还没有使用成功oc get用这两个选项--sort-by,并-o jsonpath在同一时间,所以我在这个版本回落到UNIX管道。

我正在使用 OpenShift v3.9。我还为 Kubernetes 标记了这个问题,因为它大概适用于kubectl(而不是oc)以类似的方式(没有-l app=my_app)。

jsonpath openshift kubernetes

7
推荐指数
1
解决办法
2294
查看次数

AWS Step Function - 向传递状态类型添加动态值

我在我的状态机中定义了以下状态。

 "loop":{
      "Type": "Pass",
      "Result":{
        "totalCount": "$.newFieldsResponse.body.count",
        "currentCount": 0,
        "step": 1
      },
      "ResultPath": "$.iteration",
      "Next":"iterateLoop"
    },
Run Code Online (Sandbox Code Playgroud)

我希望状态的输出是:

"newFieldsResponse": {
      "isSuccess": true,
      "error": "",
      "body": {
        "count": 2,
        "fields": [...]
      }
    },
    "iteration": {
      "totalCount": 5,
      "currentCount": 0,
      "step": 1
    }
  }
Run Code Online (Sandbox Code Playgroud)

迭代属性被添加到输入中,totalCount 属性被设置为字段数组中的项目数。

但是,“迭代”属性的输出设置为:

"iteration": {
      "totalCount": "$.newFieldsResponse.body.count",
      "currentCount": 0,
      "step": 1
    }
Run Code Online (Sandbox Code Playgroud)

看起来值 "$.newFieldsResponse.body.count" 没有得到解析,而是按原样输出。

有什么我做错了吗?有人可以就如何使其工作提出建议吗?

json jsonpath amazon-web-services aws-step-functions

7
推荐指数
3
解决办法
1万
查看次数

是否有任何适当的匹配器来解析和比较来自 MockMvc 的 Json 响应中的 LocalDateTime 字段

我正在测试getSpringBoot 控制器的方法,该方法提供在特定时间范围内以基础形式编写的对象。

\n\n

我意识到我可以在mockMvc执行后获取json并使用对象映射器解析它,使用一些流和一个断言,但我想知道是否有内置方法可以使用andExpect()序列来制作它。

\n\n

我尝试过 Hamcrest 日期匹配器,但它无法解析 LocalDateTime 格式\n\njava.lang.AssertionError: JSON path "data.SENT[0].sentAt"\nExpected: the date is within 10 days of "08 \xd0\xb0\xd0\xbf\xd1\x80 2019 19:03:48 614ms +0300"\n but: was "2019-04-02T11:36:16.51"\n

\n\n
this.mockMvc.perform(get(BASE_URL)\n                .accept(MediaType.APPLICATION_JSON_VALUE)\n                .contentType(MediaType.APPLICATION_JSON)\n                .content(jsonMockObjectMapper.writeValueAsString(smsStatisticFullRequest)))\n                .andExpect(status().isOk())\n                .andExpect(jsonPath("data.SENT[*].sentAt", Matchers.hasItems("2019-04-02T11:36:16.51")))\n//              .andExpect(jsonPath("data.SENT[0].sentAt", DateMatchers.within(10, TimeUnit.DAYS, Timestamp.valueOf(LocalDateTime.now()))))\n//              .andExpect(jsonPath("data.SENT[0].sentAt", DateMatchers.before(Timestamp.valueOf(LocalDateTime.now()))))\n                .andDo(CustomResultHandler.handleResult(name.getMethodName(), MockMvcRestDocumentation::document));\n
Run Code Online (Sandbox Code Playgroud)\n\n

我希望能够检查返回数据中的所有对象是否在断​​言时间范围内。

\n\n
Content-Type: application/json;charset=UTF-8\nCache-Control: no-cache, no-store, max-age=0, must-revalidate\n\n{\n  "apiVersion" : "1.0.1",\n  "error" : false,\n  "data" : {\n    "SENT" : [ {\n      "id" : 3,\n      "phone" : "9111233456",\n      "userId" : 683581,\n      "sentAt" …
Run Code Online (Sandbox Code Playgroud)

java testing matcher jsonpath mockmvc

7
推荐指数
1
解决办法
3234
查看次数