Created
November 21, 2018 11:58
-
-
Save MatrixManAtYrService/f00c9f7a9be9ee007faebe3cefb21880 to your computer and use it in GitHub Desktop.
Converting tabular output to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires sqawk, datamash, and jq | |
# input tab delmited columns, newline delmited rows: | |
# foo bar | |
# 1 a | |
# 2 b | |
# tbr2j (rows to json) | |
#[ | |
# { | |
# "foo": "1", | |
# "bar": "a" | |
# }, | |
# { | |
# "foo": "2", | |
# "bar": "b" | |
# } | |
#] | |
# tbc2j (columns to json) | |
#{ | |
# "foo": [ | |
# "1", | |
# "2" | |
# ], | |
# "bar": [ | |
# "a", | |
# "b" | |
# ] | |
#} | |
# sqawk adds extra fields, remove them (https://github.com/dbohdan/sqawk/issues/13) | |
desqawk() { | |
cat - \ | |
| jq 'map(del(.anf, .anr) | |
| to_entries | |
| map(select(.key | test("^a\\d+$") | not)) | |
| from_entries)' | |
} | |
# get a list of dictionaries, one per row | |
tbr2j() { | |
cat - \ | |
| sqawk -output json 'select * from a' header=1 \ | |
| desqawk | |
} | |
# get a single object with a list of values for each column | |
tbc2j() { | |
cat - \ | |
| datamash -W transpose \ | |
| awk '{ if (NR==1) | |
{ | |
printf "key\t" | |
for (i=1 ; i < NF ; i++) | |
{ | |
printf "value"i"\t" | |
} | |
print "\n"$0 | |
} | |
else | |
{ | |
print $0 | |
} | |
}' \ | |
| tbr2j \ | |
| jq 'map({(.key) : to_entries | |
| map(select(.key | match("value")).value) }) | |
| add' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment