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

时间:2013-08-28 17:18:54

标签: json kettle jsonpath

我目前正在使用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字段?

1 个答案:

答案 0 :(得分:1)

终于明白了:

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