Skip to content

Instantly share code, notes, and snippets.

@thedward
Created July 27, 2022 16:06
Show Gist options
  • Save thedward/6b7f354c3b3a8d81e4e8cf0b1dd69913 to your computer and use it in GitHub Desktop.
Save thedward/6b7f354c3b3a8d81e4e8cf0b1dd69913 to your computer and use it in GitHub Desktop.
Simple csvtojson script with jq
#!/usr/bin/env -S jq -Rnf
def csvarray:split(",")|map(fromjson);
def objectify($ks): [$ks,(.|csvarray)]|[transpose[] as [$key,$value]|{$key,$value}]|from_entries;
(input|csvarray) as $keys|[inputs|objectify($keys)|.csv_filename=input_filename]
| reduce .[] as $o ({}; .[$o.csv_filename]+=[($o|del(.csv_filename))])
| if length == 1 and has("<stdin>") then ."<stdin>" else . end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment