json JOLT转型:需要值列表中的帮助

yxyvkwin  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(102)

我试图通过将列表值从输入JSON带到主对象来将JSON转换为JSON,但我发现语法很混乱。

输入JSON:

[
  {
    "Request": {
      "Id": "id1",
      "name": "5-221734537593",
      "CreatedBy": "abc",
      "Updated": "02/28/2023 19:48:41",
      "UpdatedBy": "xyz",
      "ListOfNotes": {
        "Notes": [
          {
            "sent": "Y",
            "Id": "id2",
            "Created": "02/28/2023 19:46:00",
            "CreatedBy": "lmn",
            "Updated": "02/28/2023 19:46:06",
            "UpdatedBy": "efg"
          },
          {
            "sent": "Y",
            "Id": "id2",
            "Created": "02/28/2023 19:46:00",
            "CreatedBy": "lmn",
            "Updated": "02/28/2023 19:46:06",
            "UpdatedBy": "efg"
          }
        ]
      },
      "ListOfActions": {
        "Actions": [
          {
            "actId": "123",
            "actionPerformed": "y",
            "actionBy": "abc"
          },
          {
            "actId": "234",
            "actionPerformed": "y",
            "actionBy": "xyz"
          }
        ]
      }
    }
  }
]

预期产出:

[
  {
    "Id": "id1",
    "name": "5-221734537593",
    "CreatedBy": "abc",
    "Updated": "02/28/2023 19:48:41",
    "UpdatedBy": "xyz"
  },
  {
    "Notes": [
      {
        "sent": "Y",
        "Id": "id2",
        "Created": "02/28/2023 19:46:00",
        "CreatedBy": "lmn",
        "Updated": "02/28/2023 19:46:06",
        "UpdatedBy": "efg"
      },
      {
        "sent": "Y",
        "Id": "id2",
        "Created": "02/28/2023 19:46:00",
        "CreatedBy": "lmn",
        "Updated": "02/28/2023 19:46:06",
        "UpdatedBy": "efg"
      }
    ]
  },
  {
    "Actions": [
      {
        "actId": "123",
        "actionPerformed": "y",
        "actionBy": "abc"
      },
      {
        "actId": "234",
        "actionPerformed": "y",
        "actionBy": "xyz"
      }
    ]
  }
]

尝试的规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Request": {
          "Id": "[#].&",
          "name": "[#].&",
          "CreatedBy": "[#].&",
          "Updated": "[#].&",
          "UpdatedBy": "[#].&"
        }
      }
    }
  }
]

我当前得到的输出缺少Notes和Actions字段,我不确定如何正确地转换它们。

[
  {
    "Id": "id1",
    "name": "5-221734537593",
    "CreatedBy": "abc",
    "Updated": "02/28/2023 19:48:41",
    "UpdatedBy": "xyz"
  }
]
jdgnovmf

jdgnovmf1#

您可以使用以下shift转换规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": { // reduce two levels starting from the outermost layer
          "*": "&",   // else case
          "List*": "" // the objects/arrays start with "List"
        }
      }
    }
  }
]
nkkqxpd9

nkkqxpd92#

如果输入JSON的顺序不同的,下面的JOLT规范可以更精确

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "[0].&",
          "List*": "[]"
        }
      }
    }
  }
]

相关问题