想要将json转换为CSV文件。 CSV文件要求每行所有订单项对象1,并且该行还应包含PURCHASEORDERNO。
尝试使用.HEADER []。PURCHASEORDERNO从LINE ARRAY访问HEADER数组的对象PURCHASEORDERNO。但是出现错误jq:错误(在:1):无法遍历null(空)
JQ命令:-
echo '{"RECEIPT":{"HEADER":[{"LINE":[{"ITEMCODE":"CF7219","QUANTITY":"6"},{"ITEMCODE":"CF7223","QUANTITY":"8"},{"ITEMCODE":"CF7223L","QUANTITY":"8"},{"ITEMCODE":"CF7419","QUANTITY":"6"},{"ITEMCODE":"CF7119","QUANTITY":"3"},{"ITEMCODE":"AP7119/7219L","QUANTITY":"4"},{"ITEMCODE":"CF7620","QUANTITY":"6"},{"ITEMCODE":"HP7313","QUANTITY":"40"}],"SUPPCUSTNAME":"CONFOIL CONTAINERS","SUPPCUSTCODE":"CON001","PROCESSDATE":"20190321","PURCHASEORDERNO":"P6254"},{"LINE":[{"ITEMCODE":"CF7219","QUANTITY":"6"},{"ITEMCODE":"CF7223","QUANTITY":"8"},{"ITEMCODE":"CF7223L","QUANTITY":"8"},{"ITEMCODE":"CF7419","QUANTITY":"6"},{"ITEMCODE":"CF7119","QUANTITY":"3"},{"ITEMCODE":"AP7119/7219L","QUANTITY":"4"},{"ITEMCODE":"CF7620","QUANTITY":"6"},{"ITEMCODE":"HP7313","QUANTITY":"40"}],"SUPPCUSTNAME":"CONFOIL CONTAINERS","SUPPCUSTCODE":"CON001","PROCESSDATE":"20190321","PURCHASEORDERNO":"P6254"}]}}'
| jq -r '.RECEIPT.HEADER[].LINE[] | ["","ANCPACMEL","ADS",.HEADER[].PURCHASEORDERNO,"",.ITEMCODE,.QUANTITY,"","","","","","","","","","","","","","","","","","","","","","","","","","","",""]|@csv'
错误:-
jq:错误(在1处):无法遍历null(空)
所需的输出:-
#CustomsEntryNo,ClientCode,Warehouse,Reference,ArrivalDate,ProductCode,Quantity,QuantityUQ,Pallets,Location,Attribute1,Attribute2,Attribute3,ExpiryDate,PackingDate,CustomsEntryLineNo,CustomsEntryDate,CustomsAddInfo,CustomsQty,CustomsUQ,CtryOfOrigin,ValueForDuty,BondedWhsQty,BondedWhsUQ,TILV,CustomsSecondQuantity,CustomsSecondUnitQty,Tariff,PrimaryPreference,CustomsThirdQuantity,CustomsThirdUnitQty,ManufacturerCode,ZoneStatus,IsFromOtherFTZWarehouse,OutwardType,OutwardType
,ANCPACMEL,ADS,P62541,,CF721911,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,CF722311,8,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,CF7223L11,8,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,CF741911,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,CF711911,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,AP7119/7219L11,4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,CF762011,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62541,,HP731311,40,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,CF7219,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,CF7223,8,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,CF7223L,8,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,CF7419,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,CF7119,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,AP7119/7219L,4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,CF7620,6,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,ANCPACMEL,ADS,P62542,,HP7313,40,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
答案 0 :(得分:1)
您必须将PURCHASEORDERNO
存储在变量中,才能将其添加到每一行。
jq -r '.RECEIPT.HEADER[] | .PURCHASEORDERNO as $pono | .LINE[] | [ $pono, .ITEMCODE, .QUANTITY ] | @csv' input
更新标头:
{
echo '#Reference,ProductCode,Quantity'
jq -r '.RECEIPT.HEADER[] | .PURCHASEORDERNO as $pono | .LINE[] | [ $pono, .ITEMCODE, .QUANTITY ] | @csv' input
} > output