改进 JOLT 规范以获得预期结果

Improve JOLT spec to get expected result

来源JSON:

[
  {
    "other": false,
    "granularity": [
      {
        "impressions": 15,
        "date": "2021-11-02"
      },
      {
        "impressions": 67,
        "date": "2021-11-03"
      }
    ],
    "metadata": {
      "adGroupId": 5792423,
      "startTime": "2021-06-29T21:00:00.000"
    }
  },
  {
    "other": false,
    "granularity": [
      {
        "impressions": 226,
        "date": "2021-11-02"
      },
      {
        "impressions": 2339,
        "date": "2021-11-03"
      }
    ],
    "metadata": {
      "adGroupId": 578101600,
      "startTime": "2021-06-29T08:05:52.176"
    }
  }
]

我试过这个规格:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "granularity": {
          "*": "[&2].&"
        },
        "metadata": {
          "adGroupId": "[&2].adGroupId"
        }
      }
    }
  }
]

结果:

[
  {
    "0": {
      "impressions": 15,
      "date": "2021-11-02"
    },
    "1": {
      "impressions": 67,
      "date": "2021-11-03"
    },
    "adGroupId": 5792423
  },
  {
    "0": {
      "impressions": 226,
      "date": "2021-11-02"
    },
    "1": {
      "impressions": 2339,
      "date": "2021-11-03"
    },
    "adGroupId": 578101600
  }
]

我想从顶级删除这些索引 0,1,...etcadGroupId 应该在每一行中。

所以我期待这样的结果:

[
  {
    "impressions": 15,
    "date": "2021-11-02",
    "adGroupId": 5792423
  },
  {
    "impressions": 67,
    "date": "2021-11-03",
    "adGroupId": 5792423
  },
  {
    "impressions": 226,
    "date": "2021-11-02",
    "adGroupId": 578101600
  },
  {
    "impressions": 2339,
    "date": "2021-11-03",
    "adGroupId": 578101600
  }
]

如何解决?谢谢!

可以收集granularity键下的键值对,同时在子键前加上[&3].&1.前缀,向上三层选取公共索引,然后去掉键最后一步的索引,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "granularity": {
          "*": {
            "*": "[&3].&1.&",
            "@(2,metadata.adGroupId)": "[&3].&1.adGroupId"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]