我正面临一个问题,使用Jolt转换来转换非常复杂的嵌套JSON.输入和输出详细信息如下所示.

Input 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
    }
  ]
}

Output Expected:

[
  {
    "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 Spec我试过了:

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

现在它来的不是很好.

有谁是震动专家,能帮我得到想要的输出吗?我想我被困在最后一步了.

推荐答案

您可以将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/上的101个是:

enter image description here

Json相关问答推荐

Jolt Transformation—如果子对象为空,则将父对象更新为空

Jolt转换问题—使用键查找匹配对象

Vega-Lite:文本笔画在外部

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

删除JSON文件的特定内容

如何在Haskell中解析JSON,其中字段的名称可以是多个值之一,但应该转换为单个Haskell类型?

Oracle JSON 查询中的动态列列表

jq 中的整个单词匹配

如何在 Apps 脚本中循环遍历 JSON 响应

Rails 控制器无需编码即可渲染 json

JSON 模式实际用于什么目的?

如何用 Xidel 正确读取这个 JSON 文件?

一起使用 Argparse 和 Json

使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

如何使用 Swift 从 NSURLSession 获取 cookie?

使用 c# 通用地展平 Json

使用 JSON 的 javascript 深拷贝

为不同类型的项目数组正确的 JSON Schema

Newtonsoft 对象 → 获取 JSON 字符串

将 Pandas 数据框转换为嵌套 JSON