Skip to content

Instantly share code, notes, and snippets.

@chb0github
Last active May 27, 2026 00:10
Show Gist options
  • Select an option

  • Save chb0github/aaa82ffd500b2cfe4ba9b9f057d2d586 to your computer and use it in GitHub Desktop.

Select an option

Save chb0github/aaa82ffd500b2cfe4ba9b9f057d2d586 to your computer and use it in GitHub Desktop.
jq utility functions: json_to_csv, pivot, unpivot, csv_to_json
def json_to_csv:
[.] | flatten |
(.[0] | keys_unsorted) as $cols |
($cols | @csv),
(.[] | [.[$cols[]]] | @csv);
def pivot:
[.] | flatten |
(map(keys[]) | unique) as $keys |
reduce (.[] | pick(.[$keys[]]) | to_entries[]) as $e ({}; .[$e.key] += [$e.value]);
def unpivot:
to_entries as $entries |
($entries | map(.value | length) | max) as $len |
[range($len) | . as $i | [$entries[] | {(.key): .value[$i]}] | add];
def csv_to_json:
# best-effort: does not handle commas or newlines inside quoted fields
split("\n") | map(select(length > 0)) |
(.[0] | split(",") | map(gsub("^\"|\"$"; ""))) as $headers |
.[1:][] | split(",") | map(gsub("^\"|\"$"; "")) |
[ [$headers, .] | transpose[] | {(.[0]): .[1]} ] | add;
# Zip a header row with data rows into objects
# Input: array where [0] is headers, [1:] are rows
# From: https://stackoverflow.com/q/75828341
def zip_headers:
.[0] as $cols |
.[1:] | map(. as $row |
$cols | with_entries({"key": .value, "value": $row[.key]})
);
# Pretty-print objects as vertical key:value aligned table
# Input: array of objects (or single object)
# From: https://stackoverflow.com/q/61056651
def vtable:
[.] | flatten |
([ .[] | keys_unsorted[] ] | map(length) | max + 1) as $max |
.[] |
(keys_unsorted as $keys |
[$keys, ($keys | map($max - length) | map(. * " " + ": ")), map(.)] |
transpose[] | map(tostring) | add
), "";
# Cross-join fields into concatenated strings
# Input: array of objects; joins all field values with separator
# From: https://stackoverflow.com/q/74047703
def crossjoin(sep):
[.[] | [.[]? // .]] |
until(has(1) | not; .[:2] |= [[.[0][] + sep + .[1][]]]) | .[][];
# Parse whitespace-delimited table (first row=headers) into objects
# Input: raw multi-line string (use -R -s flags)
# From: https://stackoverflow.com/q/75839056
def table_to_json:
split("\n") | map(select(length > 0)) |
map([splits("\\s+")]) |
.[0] as $headers |
.[1:] | map([$headers, .] | transpose | map({(.[0]): .[1]}) | add);
# Group raw key-value lines into objects (e.g. netrc, config blocks)
# Input: raw string; arg $n = number of lines per record
# From: https://stackoverflow.com/q/77199004
def group_kvlines(n):
[splits("\\s+")] | {(.[0]): .[1]} | _nwise(n) | add;
# Null-safe string cleanup: gsub that passes through null
# From: https://stackoverflow.com/q/79135057
def clean_str:
(gsub("(\r\n)|\t"; ""))? // null;
# Map array to object with 1-based string index keys
# From: https://stackoverflow.com/q/63889946
def index_map:
with_entries(.key |= (1 + . | tostring));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment