需要 json 输入(嵌套数组)和输出的 jolt 规范

问题描述 投票:0回答:1

我有一个包含嵌套数组的输入 JSON。我需要 jolt 规范文件,该文件仅允许输出 json 中的特定字段,其余字段将被忽略。因此,我需要指定每个字段,以便只有这些字段才会显示在输出中,任何额外不需要的字段都将被忽略。

我编写的 jolt 规范为我提供了嵌套数组部分的所有元素,位于标签“drops”下,如下所示,但是

"drops" : [ {
          "gross" : [ "12345", "9876", "4567" ],
          "amount" : [ "098", "12", "08798" ],
          "actual" : [ 0, 0, 0 ]
        } ]

但我需要它

 "drops": [
            {
              "gross": "12345",
              "amount": "098",
              "actual": 0
            }
          ]

输入Json:

{
  "metadata": {
    "version": "1"
  },
  "message": {
    "id": "0987",
    "code": "ABCD",
    "CHECK": {
      "id": "6578",
      "date": "2024-05-16",
      "summary": [
        {
          "actual": "0",
          "type": "processed"
        },
        {
          "actual": "1",
          "type": "cancel"
        }
      ],
      "group_summary": {
        "total": 7,
        "cancel": 5,
        "hold": 1
      },
      "shift": [
        {
          "id": "09876",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "12345",
              "amount": "098",
              "actual": 0
            }
          ]
        },
        {
          "id": "09875",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "9876",
              "amount": "12",
              "actual": 0
            }
          ]
        },
        {
          "id": "45678",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "4567",
              "amount": "08798",
              "actual": 0
            }
          ]
        }
      ],
      "occupy": 1,
      "ownership": 2
    },
    "timezone": ""
  }
}

输出我需要的json:

{
  "metadata": {
    "version": "1"
  },
  "message": {
    "id": "0987",
    "code": "ABCD",
    "CHECK": {
      "id": "6578",
      "date": "2024-05-16",
      "summary": [
        {
          "actual": "0",
          "type": "processed"
        },
        {
          "actual": "1",
          "type": "cancel"
        }
      ],
      "group_summary": {
        "total": 7,
        "cancel": 5,
        "hold": 1
      },
      "shift": [
        {
          "id": "09876",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "12345",
              "amount": "098",
              "actual": 0
            }
          ]
        },
        {
          "id": "09875",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "9876",
              "amount": "12",
              "actual": 0
            }
          ]
        },
        {
          "id": "45678",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "4567",
              "amount": "08798",
              "actual": 0
            }
          ]
        }
      ],
      "occupy": 1,
      "ownership": 2
    },
    "timezone": ""
  }
}

我尝试过的 Jolt 规格

[
  {
    "operation": "shift",
    "spec": {
      "metadata": {
        "version": ["metadata.version"]
      },
      "message": {
        "id": ["message.id"],
        "code": ["message.code"],
        "CHECK": {
          "id": ["message.CHECK.id"],
          "date": ["message.CHECK.date"],
          "summary": {
            "*": {
              "actual": "message.CHECK.summary[&1].actual",
              "type": "message.CHECK.summary[&1].type"
            }
          },
          "group_summary": {
            "total": "message.CHECK.group_summary[&1].total",
            "cancel": "message.CHECK.group_summary[&1].cancel",
            "hold": "message.CHECK.group_summary[&1].hold"
          },
          "shift": {
            "*": {
              "id": "message.CHECK.shift[&1].id",
              "date": "message.CHECK.shift[&1].date",
              "drops": {
                "*": {
                  "gross": "message.CHECK.shift[&1].drops[&1].gross",
                  "amount": "message.CHECK.shift[&1].drops[&1].amount",
                  "actual": "message.CHECK.shift[&1].drops[&1].actual"
                }
              }
            }
          },
          "occupy": ["message.CHECK.occupy"],
          "ownership": ["message.CHECK.ownership"]
        },
        "timezone": ["message.timezone"]
      }
    }
  }
]


json jolt
1个回答
0
投票

似乎您只想返回移位数组的 first 组件,因此只需选择

"shift"
节点下的零索引,例如

          "shift": {
            "0": {
              "id": "message.CHECK.shift[&1].id",
              "date": "message.CHECK.shift[&1].date",
              "drops": {
                "*": {
                  "gross": "message.CHECK.shift[&1].drops[&1].gross",
                  "amount": "message.CHECK.shift[&1].drops[&1].amount",
                  "actual": "message.CHECK.shift[&1].drops[&1].actual"
                }
              }
            }
          }
© www.soinside.com 2019 - 2024. All rights reserved.