我正在尝试为我的登录程序创建模拟.我使用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数组,我想像这样应用两个过滤器:
$._embedded.values[0]._embedded.data[?(@.var1='value1' && @.var2='value2')]
Run Code Online (Sandbox Code Playgroud)
我只需要从数组中选择满足AND运算的那些元素.但是,在实践中,这似乎不起作用.
是可以这样做还是我必须执行两步操作来提取一组然后再次过滤以获得我的最终结果?
这是我的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)
我究竟做错了什么?或者,有更好的方法吗?
考虑这个示例 JSON:
{
"thing": [
{
"name": "foo",
"flag": "yep"
},
{
"name": "bar"
},
{
"name": "baz",
"flag": "nope"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我想找到所有具有相应“标志”的“名称”元素,我可以使用如下内容:
$.thing[?(@.flag)].name
Run Code Online (Sandbox Code Playgroud)
我会得到结果:
'0' => "foo"
'1' => "baz"
Run Code Online (Sandbox Code Playgroud)
但是,如果我想找到所有没有相应“标志”的“名称”元素怎么办?
(就这个问题而言,我不关心“标志”的值,只关心它是否存在)
在给定的json文档中,如何验证json路径是否存在?
我正在使用jayway-jsonpath并拥有以下代码
JsonPath.read(jsonDocument, jsonPath)
Run Code Online (Sandbox Code Playgroud)
上面的代码可能会抛出异常
com.jayway.jsonpath.PathNotFoundException:路径没有结果:$ ['abc']
为了减轻它,我打算在尝试使用JsonPath.read读取它之前验证路径是否存在
作为参考,我经历了以下两个文档,但无法真正得到我想要的.
我有一个JSON这样的文件:
{
"objects": [{
"type": "FirstType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheFirstType",
"font": "18px arial"
},
"id": "18e"
},
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
}
Run Code Online (Sandbox Code Playgroud)
我的目标是删除 Fe中某个type和 的节点,如果我想删除命名和的元素。我会得到:iddetailstypeFirstTypeid1
{
"objects": [
(...)
{
"type": "SecondType",
(...)
"details": {
"id": 1,
"name": "FirstElementOfTheSecondType",
"font": "18px arial"
},
"id": "18f"
}
],
"background": "#ffffff"
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用jsonpath解析PHP中的JSON ....
我的JSON来自于此
https://servizionline.sanita.fvg.it/tempiAttesaService/tempiAttesaPs
(这里剪切/粘贴太长了,但你可以在浏览器会话中看到它......)
JSON是一个有效的JSON(我已经使用https://jsonlint.com/ ... 验证了它).
我已经尝试使用http://www.jsonquerytool.com/的jsonpath表达式,所有看起来都很好,但是当我把所有的PHP代码示例放在下面....
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('json.php'); // JSON parser
require_once('jsonpath-0.8.0.php'); // JSONPath evaluator
$url = 'https://servizionline.sanita.fvg.it/tempiAttesaService/tempiAttesaPs';
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PROXY, '');
$data = curl_exec($ch);
curl_close($ch);
$parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$o = $parser->decode($data);
$xpath_for_parsing = '$..aziende[?(@.descrizione=="A.S.U.I. - Trieste")]..prontoSoccorsi[?(@.descrizione=="Pronto Soccorso e Terapia Urgenza Trieste")]..dipartimenti[?(@.descrizione=="Pronto Soccorso Maggiore")]..codiciColore[?(@.descrizione=="Bianco")]..situazionePazienti..numeroPazientiInAttesa';
$match1 = jsonPath($o, $xpath_for_parsing);
//print_r($match1);
$match1_encoded = $parser->encode($match1);
print_r($match1_encoded); …Run Code Online (Sandbox Code Playgroud) 我正在使用 MockMvc 为我的 spring 应用程序编写测试。假设我的 json 结果将具有以下格式:
{
"available": true,
"location": [
{"ID": 1, "path": "local1"},
{"ID": 2, "path": "local2"},
{"ID": 3, "path": "local3"}
],
"firstItem": "local1"
}
Run Code Online (Sandbox Code Playgroud)
我想测试一下,如果$.firstItem属性的值等于$.location[0].path或不等于,实际上它们应该是相等的。我应该在下面的第三个期望中放入哪个期望?
mockMvc.perform(get(url))
.andExpect(jsonPath("$.available", equalTo(true)))
.andExpect(jsonPath("$.location", hasSize(3)))
.andExpect(jsonPath("$.firstItem", ????));
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!
我们将User数据存储到MongoDB中.客户端提供JSONPath查询以请求过滤的用户集.
例如:
$.users[@.salary > 10000]
是一个JSONPath查询,用于检索工资大于10000的用户.
数据:
"users": [ {
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"salary" : 5000,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
},
........
]
}
Run Code Online (Sandbox Code Playgroud)
然而,同样的MongoQuery将是:
db.inventory.find( { salary: { $gt: 10000 } })
Run Code Online (Sandbox Code Playgroud)
我们的Java应用程序代码连接到MongoDB.
有没有什么办法可以直接运行这个JSONPath查询到MongoDB?
使用Mongo查询意味着将JSONPath查询转换为MongoQuery,这将在代码中进行繁琐的翻译.一个非常强力的方法是让所有用户首先在AS(应用程序)上,然后转换List<Document>为Json字符串,然后运行JSONPath查询.
任何帮助,将不胜感激.
我需要找到所有副本仍在启动的 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)