使用jolt转换展平具有公共列JSON的复杂嵌套

pkln4tw6  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(106)

我遇到了一个问题,使用jolt转换转换一个非常复杂的嵌套JSON。输入和输出细节如下所示。

输入JSON格式

{
  "shipment": {
    "id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "identifiers": [
      {
        "type": "CONTAINER_ID",
        "value": "MEDU6701823"
      },
      {
        "type": "BILL_OF_LADING",
        "value": "MEDUH5427560"
      },
      {
        "type": "HOUSE_BILL_OF_LADING",
        "value": "HKGB85746"
      },
      {
        "type": "CONTAINER_TYPE",
        "value": "22GP"
      },
      {
        "type": "CARRIER_SCAC",
        "value": "DHLC"
      }
    ],
    "attributes": [
      {
        "name": "REGION",
        "values": [
          "HONG KONG"
        ]
      },
      {
        "name": "ASN",
        "values": [
          "HKGB85746"
        ]
      }
    ],
    "shipmentShareLink": "https://na12.voc.project44.com/portal/v2/public/ma/tracking-details?shipmentShareToken=\"977a7cbd-b7af-4db1-95bb-ab9fe6b3228d\"",
    "createdDateTime": "2023-06-12T08:20:39+0000",
    "lastModifiedDateTime": "2023-06-27T21:27:08+0000",
    "routeInfo": {
      "stops": [
        {
          "id": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
          "type": "PICKUP",
          "location": {
            "name": "Kwai Chung",
            "identifiers": [
              {
                "type": "PORT_UN_LOCODE",
                "value": "HKKCO"
              }
            ],
            "address": {
              "city": "Kwai Chung",
              "state": "Tsuen Wan",
              "country": "HK"
            },
            "coordinates": {
              "latitude": 22.36828,
              "longitude": 114.13877
            },
            "timeZone": "Asia/Hong_Kong"
          },
          "stopSequence": 1
        },
        {
          "id": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
          "type": "PORT_OF_LOADING",
          "location": {
            "name": "Hong Kong",
            "identifiers": [
              {
                "type": "PORT_UN_LOCODE",
                "value": "HKHKG"
              }
            ],
            "address": {
              "city": "Hong Kong",
              "state": "Central and Western",
              "country": "HK"
            },
            "coordinates": {
              "latitude": 22.236988,
              "longitude": 114.16443
            },
            "timeZone": "Asia/Hong_Kong"
          },
          "stopSequence": 2
        },
        {
          "id": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
          "type": "PORT_OF_DISCHARGE",
          "location": {
            "name": "Melbourne",
            "identifiers": [
              {
                "type": "PORT_UN_LOCODE",
                "value": "AUMEL"
              }
            ],
            "address": {
              "city": "Melbourne",
              "state": "Victoria",
              "country": "AU"
            },
            "coordinates": {
              "latitude": -37.863197,
              "longitude": 144.9155
            },
            "timeZone": "Australia/Melbourne"
          },
          "stopSequence": 3
        },
        {
          "id": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
          "type": "DESTINATION",
          "location": {
            "name": "Mulgrave",
            "identifiers": [
              {
                "type": "PORT_UN_LOCODE",
                "value": "AUMUL"
              }
            ],
            "address": {
              "city": "Mulgrave",
              "state": "Victoria",
              "country": "AU"
            },
            "coordinates": {
              "latitude": -37.91667,
              "longitude": 145.2
            },
            "timeZone": "Australia/Melbourne"
          },
          "stopSequence": 4
        }
      ],
      "routeSegments": [
        {
          "id": "083fe93b-f770-31b3-87e4-caac4a3b01ed",
          "fromStopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
          "toStopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
          "transportationMode": "UNKNOWN",
          "routeSegmentSeq": 1
        },
        {
          "id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
          "fromStopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
          "toStopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
          "transportationMode": "OCEAN",
          "routeSegmentSeq": 2,
          "identifiers": [
            {
              "type": "VOYAGE_NUMBER",
              "value": "KQ321A"
            },
            {
              "type": "VESSEL_NAME",
              "value": "MATSON NIIHAU"
            },
            {
              "type": "VESSEL_IMO",
              "value": "9294159"
            },
            {
              "type": "CARRIER_NAME",
              "value": "DHL"
            }
          ]
        },
        {
          "id": "ef339c18-46e5-3a53-ab53-675f7c2cca17",
          "fromStopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
          "toStopId": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
          "transportationMode": "UNKNOWN",
          "routeSegmentSeq": 3
        }
      ]
    },
    "relatedShipments": [
      {
        "id": "ce39ccbd-2c3c-43c5-aadd-2998fdf1ac65",
        "identifiers": [
          {
            "type": "CARRIER_SCAC",
            "value": "DHLC"
          },
          {
            "type": "BILL_OF_LADING",
            "value": "HKGB85746"
          }
        ],
        "attributes": [
          {
            "name": "REGION",
            "values": [
              "HONG KONG"
            ]
          },
          {
            "name": "ASN",
            "values": [
              "HKGB85746"
            ]
          }
        ],
        "shipmentShareLink": "https://na12.voc.project44.com/portal/v2/public/ma/tracking-details?shipmentShareToken=\"8110ad80-acbc-493a-86f2-e39fb01dad6f\"",
        "createdDateTime": "2023-06-06T23:51:46+0000",
        "lastModifiedDateTime": "2023-06-27T21:27:08+0000"
      }
    ]
  },
  "states": [
    {
      "type": "IN_TRANSIT",
      "startDateTime": "2023-05-30T16:56:00+0000",
      "endDateTime": "2023-06-06T10:31:00+0000",
      "stopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
      "routeSegmentId": "083fe93b-f770-31b3-87e4-caac4a3b01ed"
    },
    {
      "type": "IN_TRANSIT",
      "startDateTime": "2023-06-06T10:31:00+0000",
      "endDateTime": "2023-06-21T04:17:00+0000",
      "stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c"
    },
    {
      "type": "AT_STOP",
      "startDateTime": "2023-06-21T04:17:00+0000",
      "stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c"
    }
  ],
  "events": [
    {
      "dateTime": "2023-05-30T16:56:00+0000",
      "receivedDateTime": "2023-06-27T18:14:00+0000",
      "type": "GATE_OUT_EMPTY",
      "stopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
      "routeSegmentId": "083fe93b-f770-31b3-87e4-caac4a3b01ed"
    },
    {
      "dateTime": "2023-06-06T10:31:00+0000",
      "receivedDateTime": "2023-06-27T18:14:00+0000",
      "type": "DEPARTURE_FROM_STOP",
      "stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c",
      "plannedDateTime": "2023-06-04T17:30:00+0000"
    },
    {
      "type": "DEPARTURE_FROM_STOP",
      "stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
      "routeSegmentId": "ef339c18-46e5-3a53-ab53-675f7c2cca17",
      "estimateDateTime": "2023-06-28T08:11:04+0000",
      "estimateLastCalculatedDateTime": "2023-06-21T08:26:50+0000"
    },
    {
      "type": "ARRIVAL_AT_STOP",
      "stopId": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
      "routeSegmentId": "ef339c18-46e5-3a53-ab53-675f7c2cca17",
      "estimateDateTime": "2023-06-29T01:25:04+0000",
      "estimateLastCalculatedDateTime": "2023-06-21T08:26:50+0000"
    }
  ],
  "positions": [
    {
      "dateTime": "2023-06-06T11:02:00+0000",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c",
      "latitude": 22.1475,
      "longitude": 114.16517
    },
    {
      "dateTime": "2023-06-06T12:19:00+0000",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c",
      "latitude": 22.116167,
      "longitude": 114.3215
    },
    {
      "dateTime": "2023-06-06T13:40:00+0000",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c",
      "latitude": 22.117666,
      "longitude": 114.48917
    },
    {
      "dateTime": "2023-06-06T15:01:00+0000",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c",
      "latitude": 22.208334,
      "longitude": 114.529335
    },
    {
      "dateTime": "2023-06-06T16:15:00+0000",
      "routeSegmentId": "5742c90f-34af-3695-8841-7de17f9fbc3c",
      "latitude": 22.3805,
      "longitude": 114.4865
    }
  ]
}

字符串

预期输出

[
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "stopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
    "stoptype": "PICKUP",
    "stop_location": "Kwai Chung",
    "stop_city": "Kwai Chung",
    "stop_state": "Tsuen Wan",
    "stop_country": "HK",
    "stop_latitude": 22.36828,
    "stop_longitude": 114.13877,
    "stop_timezone": "Asia/Hong_Kong",
    "stop_sequence": 1,
    "stop_location_identifier_type": "PORT_UN_LOCODE",
    "stop_location_identifier_value": "HKKCO"
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "stoptype": "PORT_OF_LOADING",
    "stop_location": "Hong Kong",
    "stop_city": "Hong Kong",
    "stop_state": "Central and Western",
    "stop_country": "HK",
    "stop_latitude": 22.36828,
    "stop_longitude": 114.16443,
    "stop_timezone": "Asia/Hong_Kong",
    "stop_sequence": 2,
    "stop_location_identifier_type": "PORT_UN_LOCODE",
    "stop_location_identifier_value": "HKHKG"
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "stoptype": "PORT_OF_DISCHARGE",
    "stop_location": "Melbourne",
    "stop_city": "Melbourne",
    "stop_state": "Victoria",
    "stop_country": "AU",
    "stop_latitude": -37.863197,
    "stop_longitude": 144.9155,
    "stop_timezone": "Australia/Melbourne",
    "stop_sequence": 3,
    "stop_location_identifier_type": "PORT_UN_LOCODE",
    "stop_location_identifier_value": "AUMEL"
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "stopId": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
    "stoptype": "DESTINATION",
    "stop_location": "Mulgrave",
    "stop_city": "Mulgrave",
    "stop_state": "Victoria",
    "stop_country": "AU",
    "stop_latitude": -37.863197,
    "stop_longitude": 145.2,
    "stop_timezone": "Australia/Melbourne",
    "stop_sequence": 4,
    "stop_location_identifier_type": "PORT_UN_LOCODE",
    "stop_location_identifier_value": "AUMUL"
  }
]

Jolt Speci tried:

[
  {
    "operation": "shift",
    "spec": {
      "shipment": {
        "routeInfo": {
          "stops": {
            "*": {
              "location": {
                "identifiers": {
                  "*": {
                    "@(7,shipment.id)": "[&1].shipment_id",
                    "@(3,id)": "[&1].stopId",
                    "@(3,type)": "[&1].stoptype",
                    "@(3,location.name)": "[&1].stop_location",
                    "@(2,address.city)": "[&1].stop_city",
                    "@(2,address.state)": "[&1].stop_state",
                    "@(2,address.country)": "[&1].stop_country",
                    "@(2,coordinates.latitude)": "[&1].stop_latitude",
                    "@(2,coordinates.longitude)": "[&1].stop_longitude",
                    "type": "[&1].stop_location_identifier_type",
                    "value": "[&1].stop_location_identifier_value",
                    "@(3,location.timeZone)": "[&1].stop_timezone",
                    "@(3,stopSequence)": "[&1].stop_sequence"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]


现在它没有正确地到来。
任何人谁是一个颠簸Maven,帮助我得到所需的输出。我想我卡在最后一步了

bfhwhh0e

bfhwhh0e1#

你可以使用stops数组的索引(用&4表示)沿着现有的identifiers数组的索引,以便分区更多的对象以获得嵌套在独立对象中的键值对:

[
  {
    "operation": "shift",
    "spec": {
      "shipment": {
        "routeInfo": {
          "stops": {
            "*": {
              "location": {
                "identifiers": {
                  "*": {
                    "@7,shipment.id": "&4_&1.shipment_id",
                    "@3,id": "&4_&1.stopId",
                    "@3,type": "&4_&1.stoptype",
                    "@3,location.name": "&4_&1.stop_&3",
                    "@2,address.city": "&4_&1.stop_city",
                    "@2,address.state": "&4_&1.stop_state",
                    "@2,address.country": "&4_&1.stop_country",
                    "@2,coordinates.latitude": "&4_&1.stop_latitude",
                    "@2,coordinates.longitude": "&4_&1.stop_longitude",
                    "type|value": "&4_&1.stop_&3_identifier_&",
                    "@3,location.timeZone": "&4_&1.stop_timezone",
                    "@3,stopSequence": "&4_&1.stop_sequence"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

字符串
网站http://jolt-demo.appspot.com/上的 * 演示 * 是:


的数据

相关问题