首页 > 解决方案 > 如何在 JOLT 中准备一个数组?

问题描述

我正在尝试从我得到的输入中准备一个新数组,但它在每个对象中循环多次。

我有这样的输入,

  {
"orders": {
  "orderItem": [
  {
    "shipperRef": null,
    "productID": "5150002516",
    "lineNumber": 1,
    "description": "SM UNCRSTBL PB&GRP 4-PK 8CT CS",
    "packageQuantity": 198,
    "packageType": {
      "description": "CASE",
      "edicode": "CAS"
    },
    "weight": 1108.8,
    "netWeight": null,
    "volume": 108.5,
    "commodity": {
      "description": "0 DEGREES OR LOWER",
      "shortDesc": null,
      "freightClass": "FAK",
      "temperatureClass": "FROZEN",
      "trailerTypes": {
        "trailerType": [
          "REEFER"
        ]
      },
      "hazmat": "No"
    },
    "ladingQuantity": 3,
    "ladingType": {
      "description": "PALLET",
      "edicode": "PLT"
    },
    "valuationRate": null,
    "valuationTotal": null

      ]
    },
    "billableItemAllocationDetails": null
  },
  {
    "shipperRef": null,
    "productID": "5150002517",
    "lineNumber": 2,
    "description": "SM UNCRSTBL PB&STR 4-PK 8CT CS",
    "packageQuantity": 132,
    "packageType": {
      "description": "CASE",
      "edicode": "CAS"
    },
    "weight": 739.2,
    "netWeight": null,
    "volume": 77.35,
    "commodity": {
      "description": "0 DEGREES OR LOWER",
      "shortDesc": null,
      "freightClass": "FAK",
      "temperatureClass": "FROZEN",
      "trailerTypes": {
        "trailerType": [
          "REEFER"
        ]
      },
      "hazmat": "No"
    },
    "ladingQuantity": 2,
    "ladingType": {
      "description": "PALLET",
      "edicode": "PLT"
    },
    "valuationRate": null,
    "valuationTotal": null,
    "payableItemAllocationDetails": {
      "itemAllocationDetail": [
        {
          "chargeCodeID": "BAS",
          "chargeCodeDesc": "BA"
        },
        {
          "chargeCodeID": "405",
          "chargeCodeDesc": "FUEL SURCHARGE",
          "currencyCode": "USD",
          "payableAllocation": 4.05,
          "segment1": "510",
          "segment2": null,
          "segment3": null,
          "segment4": null,
          "segment5": null,
          "segment6": null
        }
      ]
    },
    "billableItemAllocationDetails": null
  }
]
 }
 }

这里的输出是,

 {
 "items": [
  {
  "id": 1,
  "shipment": {
    "type": "test"
  }
},
{
  "id": 2,
  "shipment": {
    "type": "test"
  }
},
{
  "id": "3",
  "shipment": {
    "type": "test"
  }
 }
]}

我的专业,

 {
"orders": {
 "order": {
   "*": {
    "orderItems": {
      "orderItem": {
        "*": {
          "lineNumber": "items[&1].id"
        }
      }
      }
    }
  }
 }
}

这些给了我这样的o/p,

"items" : [ {
  "id" : [ "1", "1" ]
  }, {
   "id" : [ "2", "2" ]
  }, {
  "id" : [ "3", "3" ]
   }, {
   "id" : [ "4", "4" ]
   }, {
  "id" : [ "5", "5" ]
   }, {
   "id" : [ "6", "6" ]
   }, {
   "id" : "7"
   } ]

我正在尝试从我得到的输入中准备一个新数组,但它在每个对象中循环多次。我不确定我在哪里做错了,任何人都可以建议我帮助解决它。谢谢。

标签: jsonjolt

解决方案


尽管问题不清楚,但希望这是一个解决方案:

    [
      {
        "operation": "shift",
        "spec": {
          "orders": {
            "orderItem": {
              "*": {
                "lineNumber": {
                  "@": "items[&2].id" // @ takes value of 'lineNumber'
                },
                "#test": "items[&1].shipment.type" // # allows to put value 'test'
              }
            }
          }
        }
      }
    ]

推荐阅读