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

mzsu5hc0  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(110)

源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,...etc从顶层。和adGroupId应该在每一行。
所以我期待这个结果:

[
  {
    "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
  }
]

如何修复?谢谢!

r6l8ljro

r6l8ljro1#

您可以收集granularity键下的键-值对,同时以[&3].&1.作为子键的前缀,以便通过向上三个级别选择公共索引,然后在最后一步删除索引的键,例如

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

相关问题