gpt4 book ai didi

当我只想要父对象时,JSONPath 查询从嵌套对象返回同名键

转载 作者:行者123 更新时间:2023-12-05 01:08:32 27 4
gpt4 key购买 nike

我目前正在使用 Pentaho Kettle 进行一些 ETL 工作,我需要集成一个 JSON 提要,这意味着我需要使用 JSONPath 来抓取数据。在大多数情况下,它运行良好,除了一些 JSON 数据是嵌套对象,在父项和子项中具有相同的字段名称。

示例 JSON:

[
{
"Key": "5e59d536-2e3c-487c-bff1-efd0a706532f",
"Product": {
"Name": "Some Product",
"LastUpdated": "2013-08-23T12:10:25.454",
},
"Reviewer": {
"Email": "blah@foo.com",
"LastUpdated": "2013-08-23T12:10:25.454",
},
"LastUpdated": "2013-08-23T12:10:25.407",
},
{
"Key": "f3ae6a4b-1a20-4a9a-9a8e-2de5949c4493",
"Product": {
"Name": "Some Product",
"LastUpdated": "2013-08-23T12:10:51.896",
},
"Reviewer": {
"Email": "blah@foo.com",
"LastUpdated": "2013-08-23T12:10:51.896",
},
"LastUpdated": "2013-08-23T12:10:51.896",
},
{
"Key": "de01c358-6c74-473c-8cd4-a44cf50132df",
"Product": {
"Name": "Some Product",
"LastUpdated": "2013-08-26T10:30:13.617",
},
"Reviewer": {
"Email": "blah@foo.com",
"LastUpdated": "2013-08-26T10:30:13.617",
},
"LastUpdated": "2013-08-26T10:30:13.601",
},
},
{
"Key": "af04e48a-3ce8-4227-a00a-14483ca75058",
"Product": {
"Name": "Some Product",
"LastUpdated": "2013-08-26T10:31:20.573",
},
"Reviewer": {
"Email": "blah@foo.com",
"LastUpdated": "2013-08-26T10:31:20.573",
},
"LastUpdated": "2013-08-26T10:31:20.573",
},
{
"Key": "d1a787bb-37d2-4ea9-84fd-5a3d454b9127",
"Product": {
"Name": "Some Product",
"LastUpdated": "2013-08-27T11:59:56.777",
},
"Reviewer": {
"Email": "blah@foo.com",
"LastUpdated": "2013-08-27T11:59:56.777",
},
"LastUpdated": "2013-08-27T11:59:56.73",
},
{
"Key": "d8646319-af27-464f-bd50-d61e035800c6",
"Product": {
"Name": "Some Product",
"LastUpdated": "2013-08-27T19:43:06.928",
},
"Reviewer": {
"Email": "blah@foo.com",
"LastUpdated": "2013-08-27T19:43:06.928",
},
"LastUpdated": "2013-08-27T19:43:06.866",
},
]

如您所见,父对象及其子对象“Product”和“Reviewer”都有“LastUpdated”字段。我试图仅获取父对象的“LastUpdated”,但使用:

$..LastUpdated

按顺序返回父 LastUpdated、Product LastUpdated,然后是 Reviewer LastUpdated。

结果:

[
"2013-08-23T12:10:25.407",
"2013-08-23T12:10:25.454",
"2013-08-23T12:10:25.454",
"2013-08-23T12:10:51.896",
"2013-08-23T12:10:51.896",
"2013-08-23T12:10:51.896",
"2013-08-26T10:30:13.601",
"2013-08-26T10:30:13.617",
"2013-08-26T10:30:13.617",
"2013-08-26T10:31:20.573",
"2013-08-26T10:31:20.573",
"2013-08-26T10:31:20.573",
"2013-08-27T11:59:56.73",
"2013-08-27T11:59:56.777",
"2013-08-27T11:59:56.777",
"2013-08-27T19:43:06.866",
"2013-08-27T19:43:06.928",
"2013-08-27T19:43:06.928"
]

预期结果:

[
"2013-08-23T12:10:25.407",
"2013-08-23T12:10:51.896",
"2013-08-26T10:30:13.601",
"2013-08-26T10:31:20.573",
"2013-08-27T11:59:56.73",
"2013-08-27T19:43:06.866",
]

有没有我可以用来仅获取父对象的 LastUpdated 字段的查询?

最佳答案

终于明白了:

$[*].LastUpdated -> only the parents
$[*].Product.LastUpdated -> only the product
$[*].Reviewer.LastUpdated -> only the reviewer

关于当我只想要父对象时,JSONPath 查询从嵌套对象返回同名键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com