Convert JSON to CSV using jq
Input JSON (products.json)
{
"products": [
{
"sku":"Carrot",
"description":"bunches of 6 to 8. Pack in 1 1/9 bu. box"
},
{
"sku":"Broccoli",
"description":"1 1/9 bu. box. Weight 23 lb. Stem on. Iced."
}
]
}
Convert using jq:
jq -r '.products | .[] | [.sku, .description] | @csv' < products.json
Limitations: No CSV header
Convert using yq
yq is a command-line YAML, JSON processor with handy default behavior. Install using homebrew:
brew install yq
Convert JSON to CSV using jq
% yq -o=csv products.json
Error: csv encoding only works for arrays, got: !!map
First attempt failed. Looks like I need to give it an expression to select “products”
% yq -o=csv '.products' products.json
sku,description
Carrot,bunches of 6 to 8. Pack in 1 1/9 bu. box
Broccoli,1 1/9 bu. box. Weight 23 lb. Stem on. Iced.
Success! More yq documentation on their website.
Be the first to leave a comment. Don’t be shy.