Skip to content
nntrn edited this page Sep 29, 2023 · 5 revisions

json2csv

jq -r '(.[0] | keys_unsorted) as $keys | ([$keys] + map([.[ $keys[] ]])) [] | @csv'

csv2json

jq -R -s 'def objectify(headers): . as $in |  reduce range(0; headers|length) as $i ({}; .[headers[$i]] = ($in[$i] | tonumber? // .) ); def csv2table: split("\n") | map( split(",") | map(sub("^ +";"") |  sub(" +$";"")) ); def csv2json: csv2table | (.[0]|map(gsub(" "; "_")|gsub("[()]";""))) as $headers | reduce ( .[1:][] | select(length > 0) ) as $row ( []; . + [ $row| objectify($headers) ]); csv2json'

Produce list of unique keys

jq -r '[paths | map(strings)] | unique[] | join(".")'

Join files on key

JOIN_KEY=...

jq --arg joinkey $JOIN_KEY 'JOIN(INDEX(inputs[];.[$joinkey]);.[];.[$joinkey];add)'

Pass argument from environment to remove recursively:

DELETE_KEY=...

jq --arg deletekey $DELETE_KEY 'walk(if type == "object" then . | del(.[$deletekey]) else . end)'

Group by count

GROUPBY_KEY=...

jq --arg groupbykey $GROUPBY_KEY '{"\($groupbykey)": group_by(.[$groupbykey]) | map({"\(.[0][$groupbykey])": length}) | add}'

Summary

jq 'def summary($item): {"\($item)":group_by(.[$item])|map({"\(.[0][$item])":length})|add}; [(.[0]|keys)[] as $keys | summary($keys)] | add'

Flatten nested object

jq 'map(. as $in | reduce leaf_paths as $path ({}; . + { ($path | map(tostring) | join(".")): $in | getpath($path) }))'

Remove spaces or periods from object key names

  • gsub("[^a-zA-Z0-9]"; "_")
  • gsub(" "; "_")

For object

jq 'to_entries|map({key:(.key|gsub(" "; "_")),value})|from_entries'

For array of objects:

jq 'map(to_entries|map({key:(.key|split(" (")[0]|gsub("[^a-zA-Z0-9]"; "_")|ascii_downcase),value})|from_entries)'

Get environment

jq -n 'env'

References:

Clone this wiki locally