Created
May 11, 2017 23:05
-
-
Save jonstefansson/36f4057c6010f332a4dd966439a07ae3 to your computer and use it in GitHub Desktop.
A jq filter that converts an array objects with a nested array into CSV-formatted output with one row for each of the nested arrays and the common from the parent object duplicated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ----------------------------------------------------------------------------- | |
# A jq filter that converts an array objects with a nested array into | |
# CSV-formatted output with one row for each of the nested arrays and the | |
# common from the parent object duplicated. | |
# ----------------------------------------------------------------------------- | |
jq -r '.Items[] | {"Sku": .Sku} + (.Locations[]|{"WarehouseCode": .WarehouseCode, "LocationCode": .LocationCode, "Quantity": .Quantity}) | ["\(.Sku)", "\(.WarehouseCode)", "\(.LocationCode)", .Quantity] | @csv' <<-EOF | |
{ | |
"Items": [ | |
{ | |
"Sku": "1301271DKR028", | |
"PrimarySku": "1301271DKR028", | |
"Code": "", | |
"PrimaryCode": "4045928338", | |
"Locations": [ | |
{ | |
"WarehouseCode": "VRN", | |
"LocationCode": "P", | |
"Quantity": 1, | |
"Reserve": false | |
} | |
] | |
}, | |
{ | |
"Sku": "1301930RIP0OS", | |
"PrimarySku": "1301930RIP0OS", | |
"Code": "", | |
"PrimaryCode": "4048566466", | |
"Locations": [] | |
}, | |
{ | |
"Sku": "1301615ANH00L", | |
"PrimarySku": "1301615ANH00L", | |
"Code": "", | |
"PrimaryCode": "4047195086", | |
"Locations": [ | |
{ | |
"WarehouseCode": "MEL", | |
"LocationCode": "GENERAL", | |
"Quantity": 1, | |
"Reserve": true | |
}, | |
{ | |
"WarehouseCode": "VRN", | |
"LocationCode": "R", | |
"Quantity": 4, | |
"Reserve": false | |
} | |
] | |
}, | |
{ | |
"Sku": "0000000VOM00M", | |
"PrimarySku": "0000000VOM00M", | |
"Code": "", | |
"PrimaryCode": "4048589780", | |
"Locations": [ | |
{ | |
"WarehouseCode": "VRN", | |
"LocationCode": "STAGING", | |
"Quantity": 36, | |
"Reserve": true | |
}, | |
{ | |
"WarehouseCode": "VAL", | |
"LocationCode": "GENERAL", | |
"Quantity": 10, | |
"Reserve": true | |
}, | |
{ | |
"WarehouseCode": "VRN", | |
"LocationCode": "T", | |
"Quantity": 14, | |
"Reserve": false | |
}, | |
{ | |
"WarehouseCode": "MEL", | |
"LocationCode": "GENERAL", | |
"Quantity": 10, | |
"Reserve": true | |
}, | |
{ | |
"WarehouseCode": "SHO", | |
"LocationCode": "GENERAL", | |
"Quantity": 10, | |
"Reserve": true | |
}, | |
{ | |
"WarehouseCode": "LES", | |
"LocationCode": "GENERAL", | |
"Quantity": 17, | |
"Reserve": true | |
} | |
] | |
} | |
] | |
} | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment