Issue
My json file is similar of this:
{
"A1": "1.2"
"A2": "3.5"
"A3": "2.6"
}
I need transform it to csv file and it looks like this:
A1,1.2
A2,3.5
A3,2.6
My code is:
jq -r 'map(.[] | tonumber) | @csv' file.json > file.csv
and my result is:
1.2,3.5,2.6
Solution
Once you fix your example JSON so it's valid:
$ jq -r 'to_entries[] | [.key, (.value | tonumber)] | @csv' input.json
"A1",1.2
"A2",3.5
"A3",2.6
to_entries
turns an object into an array of objects with the key
field holding the name of one of the original object's keys and value
its corresponding value. Then turn each of those objects into a two-element array which is fed to @csv
.
Answered By - Shawn Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.