json 当数组中的第二个元素有嵌套数组时要处理的动态抖动

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

当一个嵌套数组出现在外部数组的第二个元素中时,处理动态Jolt规范
如果我们没有收到嵌套数组,我的jolt规范工作得非常好
一些传入的JSON在routeSegements数组中有一个嵌套的标识符数组。有些元素没有

输入

{
  "shipment": {
    "id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "routeInfo": {
      "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
        }
      ]
    }
  }
}

字符串

预期输出

[
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "083fe93b-f770-31b3-87e4-caac4a3b01ed",
    "route_segments_from_stopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
    "route_segments_to_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "transportation_mode": "UNKNOWN",
    "route_segment_seq_no": 1
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "VOYAGE_NUMBER",
    "route_segments_identifier_value": "KQ321A",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "VESSEL_NAME",
    "route_segments_identifier_value": "MATSON NIIHAU",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "VESSEL_IMO",
    "route_segments_identifier_value": "9294159",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "CARRIER_NAME",
    "route_segments_identifier_value": "DHL",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "ef339c18-46e5-3a53-ab53-675f7c2cca17",
    "route_segments_from_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "route_segments_to_stopId": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
    "transportation_mode": "UNKNOWN",
    "route_segment_seq_no": 3
  }
]

Jolt Speci tried:

[
  {
    "operation": "shift",
    "spec": {
      "shipment": {
        "id": "[&1].shipment_id",
        "routeInfo": {
          "routeSegments": {
            "*": {
              "id": "[&1].route_segments_id",
              "fromStopId": "[&1].route_segments_from_stopId",
              "toStopId": "[&1].route_segments_to_stopId",
              "transportationMode": "[&1].transportation_mode",
              "identifiers": {
                "*": {
                  "type": "[&1].route_segments_identifier_type",
                  "value": "[&1].route_segments_identifier_value"
                }
              },
              "routeSegmentSeq": "[&1].route_segment_seq_no"
            }
          }
        }
      }
    }
  }
]


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

px9o7tmv

px9o7tmv1#

您可以再添加一个分组级别,即右侧标识符前面的routeSegmentSeq@1,routeSegmentSeq.@3,routeSegmentSeq.)的值,例如

[
  {
    "operation": "shift",
    "spec": {
      "@shipment.routeInfo.routeSegments": {
        "*": {
          "@2,shipment.id": "@1,routeSegmentSeq.&1.shipment_id",
          "id": "@1,routeSegmentSeq.&1.route_segments_id",
          "fromStopId": "@1,routeSegmentSeq.&1.route_segments_from_stopId",
          "toStopId": "@1,routeSegmentSeq.&1.route_segments_to_stopId",
          "routeSegmentSeq": "@1,routeSegmentSeq.&1.route_segment_seq_no",
          "transportationMode": "@1,routeSegmentSeq.&1.transportationMode",
          "identifiers": {
            "*": {
              "@4,shipment.id": "@3,routeSegmentSeq.&1.shipment_id",
              "@2,id": "@3,routeSegmentSeq.&1.route_segments_id",
              "@2,fromStopId": "@3,routeSegmentSeq.&1.route_segments_from_stopId",
              "@2,toStopId": "@3,routeSegmentSeq.&1.route_segments_to_stopId",
              "*": "@3,routeSegmentSeq.&1.route_segments_identifier_&",
              "@2,routeSegmentSeq": "@3,routeSegmentSeq.&1.route_segment_seq_no",
              "@2,transportationMode": "@3,routeSegmentSeq.&1.transportationMode"
            }
          }
        }
      }
    }
  },
  { // get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  },
  { // pick only single one from repeating identical values
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]

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


的数据

相关问题