jq error “object is not valid in a csv row”

Viewed 882

I'm trying to convert a json object into csv via jq. The following is the sample structure:

[
  "193.176.211.70",
  [
    "2697131889764497b2beef1bac8e742d"
  ]
]
[
  "193.176.211.70",
  [
    "2697131889764497b2beef1bac8e742d"
  ]
]
[
  "205.169.39.30",
  []
]
[
  "65.155.30.101",
  []
]
[
  "45.64.241.129",
  [
    "2697131889764497b2beef1bac8e742d"
  ]
]
[
  "45.64.241.129",
  [
    "2697131889764497b2beef1bac8e742d"
  ]
]

Tried with jq -r '. | @csv' output2.json > output.csv but it returns jq: error (at output2.json:9486): array (["269713188...) is not valid in a csv row

Referring to the only question related to jq on stackoverflow, is not working either Any advice?

1 Answers

You need to use flatten to explode/flatten the arrays first, before you can store the data as CSV. This will create a record for each element of the array.

$ jq -r 'flatten | @csv' output2.json
"193.176.211.70","2697131889764497b2beef1bac8e742d"
"193.176.211.70","2697131889764497b2beef1bac8e742d"
"205.169.39.30"
"65.155.30.101"
"45.64.241.129","2697131889764497b2beef1bac8e742d"
"45.64.241.129","2697131889764497b2beef1bac8e742d"
Related