Repro:
$ echo '{"name":"250/778/236","province":"BC","location_hint":"Kelowna/Victoria","notes":"Northern BC"}' | jq '[.] | @csv'
jq: error (at <stdin>:1): object ({"name":"25...) is not valid in a csv row
So we got a fairly vanilla object here, key-value. (Originally I had an array of those) JQ won't turn it to csv because... well the error doesn't tell me that.
Enhancement request to make the error message more helpful. (Second, I don't even know the problem)
@joallard - Sometimes you just have to believe what the error message says.
In fact, @csv can only be used validly when the input is an array of scalars (in the sense of the built-in filter namedscalars).
I think there would be a reasonable argument for making @csv accept an array of objects whose values are scalars, with the keys of the objects being used to form the header of the CSV.
Note that on the first page of jq questions on StackOverflow, there are three different questions on how to do this; and my answer for one of them is my most upvoted answer.
@pkoppstein Well I believed it, it just didn't tell me much in the way of fixing it, which is pretty much what an error message should do. I'll also echo @slapresta's argument for an enhancement request, but that could be another ticket.
On the topic of the error, what you told me is way more instructive. Eg.:
Objects can't be serialized into csv rows; only arrays of values can be made into rows. Maybe you meant to call the
x_valuesfilter on your row?
Or something similar. (I haven't found how to get values out of objects, I just know that values ain't it)
Interestingly, earlier this week I read an interesting article about the design of errors in Rust. It doesn't map 1-to-1, but it makes the point that errors should be as helpful as possible to help fix your problem.
Ha. 4 years later, I googled this, and went "see? someone else also has that problem". Imagine my face when I landed here and saw that the OP was me.
And as I'm writing this, I'm stuck at the same place, values doesn't give me what I need in order to give it back to csv.
(15 minutes later...)
One of the worst caveats being that if you don't include .[] at the end of the filter, you get the most unhelpful array (["key...) is not valid in a csv row
Here's what I ended up needing:
[(.[0] | keys), (map(to_entries | map(.value)) | .[])] | .[] | @csv
That will turn an array of objects into a CSV.
Edit: Wait no, that's not it. But at this point, I'm reconsidering whether I really really need to be converting this data. Probably not.
See you in 2024, me!
Most helpful comment
@pkoppstein Well I believed it, it just didn't tell me much in the way of fixing it, which is pretty much what an error message should do. I'll also echo @slapresta's argument for an enhancement request, but that could be another ticket.
On the topic of the error, what you told me is way more instructive. Eg.:
Or something similar. (I haven't found how to get values out of objects, I just know that
valuesain't it)Interestingly, earlier this week I read an interesting article about the design of errors in Rust. It doesn't map 1-to-1, but it makes the point that errors should be as helpful as possible to help fix your problem.