我是 Javascript 世界的新手,需要对 JSONPath 进行一些澄清。 http://goessner.net/articles/JsonPath/
我了解 JSONPath 用于解析 JSON 结构并获取特定值。我的问题是,是否有可能扭转这种行为?例如,假设我在表格/网格中显示文章中给出的“商店”中的“书籍”列表,并且单击时有一个“创建”按钮,获取“类别”、“作者”的输入'、'书名'、'isbn' 和 '价格'。现在我知道这些输入元素的 JSONPaths。是否可以使用给定的 JSONPaths 创建 JSON 结构?有没有工具可以做到这一点?或者我只是错过了一些基本的东西?
问候 BK
根据this,没有等效的语法吗?
有办法实现这个目标吗?
例子
{ ...
attrib1: {},
attrib2: []
...
}
Run Code Online (Sandbox Code Playgroud)
当我在attrib2路径中循环时,我想从 获取一些信息attrib1。在这种情况下我该怎么做?
我正在使用 JsonPath 来测试 API 的 REST 端点。我的测试用例看起来像这样。
首先我创建新对象
class Item {
String name;
int id;
List<String> places;
}
String name = "Randomname" + new Random().nextLong();
JSONObject obj = new Item(name, 3, Arrays.asList("Rome3", "London3", "Paris3"));
post("/items").content(obj);
Run Code Online (Sandbox Code Playgroud)
然后我得到所有项目的列表,并检查我刚刚创建的项目是否在列表中。
mockMvc.perform(get("/api/v1/test"))
.andExpect(jsonPath("$.data[*].name", hasItems("item 1", "item 2"))) // Fine
.andExpect(jsonPath("$.data[?(@.id == 1)].name", contains("item 1")))
.andExpect(jsonPath("$.data[?(@.id == 1)].places", contains(contains("Rome1", "London1", "Paris1"))))
.andReturn();
Run Code Online (Sandbox Code Playgroud)
这可行,但我认为我做得不对,因为我有一个嵌套的contains(contains()). (我已将完整的 JSON 响应放在本文末尾。)
问题是因为 JsonPath 表达式返回一个列表,所以我需要使用列表匹配器。例如,不要写这个
jsonPath("$.data[?(@.id == 1)].name", is("item 1"))
Run Code Online (Sandbox Code Playgroud)
我需要这样写
jsonPath("$.data[?(@.id == 1)].name", contains("item 1"))
Run Code Online (Sandbox Code Playgroud)
因为 JsonPath 表达式将返回一个包含一个元素的 JSON …
给定如下 JSON:
{
"a":1,
"b":2,
"c":3,
"d":4,
"e":5
}
Run Code Online (Sandbox Code Playgroud)
如何选择b,d和e以获得以下 JSON?
{
"b":2,
"d":4,
"e":5
}
Run Code Online (Sandbox Code Playgroud)
我想要一个 JSON 对象,而不仅仅是2,4和5值?
这就是我正在尝试但失败的:
$.[b,d,e]
Run Code Online (Sandbox Code Playgroud) 我有一组看起来像这样的 Json 数据文件
[
{"client":"toys",
"filename":"toy1.csv",
"file_row_number":1,
"secondary_db_index":"4050",
"processed_timestamp":1535004075,
"processed_datetime":"2018-08-23T06:01:15+0000",
"entity_id":"4050",
"entity_name":"4050",
"is_emailable":false,
"is_txtable":false,
"is_loadable":false}
]
Run Code Online (Sandbox Code Playgroud)
我使用以下自定义分类器 Json Path 创建了一个 Glue Crawler
$[*]
Run Code Online (Sandbox Code Playgroud)
Glue 返回正确标识的列的正确模式。
但是,当我在 Athena 上查询数据时……所有数据都在第一列中,其余列都是空的。
我怎样才能让数据根据他们的列传播?
谢谢!
有人可以告诉我为什么这不起作用:
@Test
public void should_parse_json() {
Expression expression = new SpelExpressionParser().parseExpression("#jsonPath(get('JsonData'), '$.someData')");
Map<String, Object> data = new HashMap<>();
data.put("JsonData", "{\"someData\": 100}");
StandardEvaluationContext context = new StandardEvaluationContext(data);
context.addPropertyAccessor(new JsonPropertyAccessor());
assertThat(expression.getValue(context, Object.class)).isEqualTo(100);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误“org.springframework.expression.spel.SpelEvaluationException:EL1006E:找不到函数‘jsonPath’”
我在类路径中有以下 jar:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
SPEL 文档对我没有帮助。
我正在尝试从 JSON 中提取一个值到flowfile-attribute。当我运行 EvaluateJsonPath 处理器时,我收到一个错误说明
"Unable to get a scalar value for expression $..fields.storyBoard.stringValue.
输入 JSON 如下所示:
{
"name" : "projects/fakedims-0000/databases/(default)/documents/device/0000",
"fields" : {
"reportKey" : {
"stringValue" : "abc123"
},
"dateOccured" : {
"timestampValue" : "2018-10-14T04:00:00Z"
},
"storyBoard" : {
"stringValue" : "https://path/to/media"
},
"new" : {
"integerValue" : "25"
},
"name" : {
"stringValue" : "device one"
},
"location" : {
"geoPointValue" : {
"latitude" : -78.413751,
"longitude" : 38.156487
}
}
},
"createTime" : …Run Code Online (Sandbox Code Playgroud) 我的需要: 我想在我的 JSON 响应中构建一个包含过滤器的路径。
//Simple path - OK
response.Node1.Node2.Node3.Node4
//Path with list - OK
response.Node1.Node2.Node3[1].Node4
//Path with condition (filter) - NOK
response.Node1.Node2[?(@.Node3 == 'value')].Node3bis
Run Code Online (Sandbox Code Playgroud)
Postman 不理解 [?(@.Node3 == 'value')] 语法,因为 jsonPath 本身不支持。
因此,我试图在 Postman 中导入 jsonPath js 库,我发现了两个:
研究
使用来自 OData v4 API 响应的以下 JSON 对象:
{
"@odata.context": "https://api.asdfasdf.com/",
"@odata.count": 2,
"value": [
{
"id": 123456789,
"name": "Random name"
},
{
"id": 15345,
"name": "Random name2"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这适用于选择数组中的对象:
$.value[?(@.id == 123456789)]
使用这个 JSONPath 我可以获得属性“@odata.count”的值:
$.['@odata.count']
但是如何使用“@odata.count”用表达式编写 JSONPath 呢?例如。仅当计数大于 1 时才选择,这些表达式失败:
$.[('@odata.count' > 1)]
$.[?('@odata.count' > 1)]
$.[(@.['@odata.count'] > 1)]
... and a lot of other expressions
Run Code Online (Sandbox Code Playgroud)
我坚信我正在研究与我想象的不同深度的 json,并且属性的命名约定(以及选择它的方式)让我发疯。
最终目标是在 SoapUI 测试套件断言中使用表达式。
将响应中的“@odata.count”属性与我们的测试数据进行比较。
编辑: 感谢评论者,通过删除数字后的引号来修复 json。
以及帮助我找到解决方案的表达式的其他发现:
这些表达式的结果是“2”:
$['@odata.count']
$..[0]['@odata.count']
Run Code Online (Sandbox Code Playgroud)
此表达式产生一个空数组:
$..[?(@['@odata.count'] > 1)]
Run Code Online (Sandbox Code Playgroud)
此表达式的结果为“null”:
?($['@odata.count'] > 1) …Run Code Online (Sandbox Code Playgroud) 当我想提取某个容器环境变量的当前值时,我可以使用 jsonpath 语法如下:
kubectl get pods -l component='somelabel' -n somenamespace -o \
jsonpath='{.items[*].spec.containers[*].env[?(@.name=="SOME_ENV_VARIABLE")].value}')
Run Code Online (Sandbox Code Playgroud)
这将返回名称为 env varialbe 的值SOME_ENV_VARIABLE。带有 json 容器环境变量的 Pod 部分将如下所示:
"spec": {
"containers": [
{
"env": [
{
"name": "SOME_ENV_VARIABLE",
"value": "some_value"
},
{
"name": "ANOTHER_ENV_VARIABLE",
"value": "another_value"
}
],
Run Code Online (Sandbox Code Playgroud)
当我想在部署中修补某些值时,我使用的命令的语法如下:
kubectl -n kube-system patch svc kubernetes-dashboard --type='json' -p="[{'op': 'replace', 'path': '/spec/ports/0/nodePort', 'value': $PORT}]"
Run Code Online (Sandbox Code Playgroud)
但是,'op': 'replace'在需要使用表达式的情况下,如何修补变量env[?(@.name=="SOME_ENV_VARIABLE")]?我应该使用哪种语法?
jsonpath ×10
json ×6
java ×2
apache-nifi ×1
aws-glue ×1
hamcrest ×1
javascript ×1
kubectl ×1
kubernetes ×1
node.js ×1
odata ×1
postman ×1
soapui ×1
spring ×1
spring-el ×1