Want to convert a json into CSV file. CSV file require all the Line Item Object 1 per line + that line should also contain PURCHASEORDERNO.

Trying to access the object PURCHASEORDERNO of the HEADER array from the LINE ARRAY by using .HEADER[].PURCHASEORDERNO. But getting error jq: error (at :1): Cannot iterate over null (null)

JQ command :-

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'


Error:-

jq: error (at :1): Cannot iterate over null (null)

Desired Output:-

#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,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

1 Answers

1
ceving On

You have to store the PURCHASEORDERNO in a variable in order to add it to each row.

jq -r '.RECEIPT.HEADER[] | .PURCHASEORDERNO as $pono | .LINE[] | [ $pono, .ITEMCODE, .QUANTITY ] | @csv' input

Update with header:

{
  echo '#Reference,ProductCode,Quantity'
  jq -r '.RECEIPT.HEADER[] | .PURCHASEORDERNO as $pono | .LINE[] | [ $pono, .ITEMCODE, .QUANTITY ] | @csv' input
} > output