Skip to content

Instantly share code, notes, and snippets.

@drjerry
Created August 26, 2012 17:22
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drjerry/3481798 to your computer and use it in GitHub Desktop.
Save drjerry/3481798 to your computer and use it in GitHub Desktop.
Converts stream of tabular records to stream of JSON records.
{
if (NR == 1) {
split($0, tags);
if (EC == "") EC = "\"";
}
else {
split($0, vals);
jrec = "{";
for (i = 1; i <= NF; ++i) {
if (vals[i] ~ /[^0-9.]/)
jrec = jrec EC tags[i] EC ":" EC vals[i] EC;
else
jrec = jrec EC tags[i] EC ":" vals[i];
if (i < NF)
jrec = jrec ", ";
else
jrec = jrec "},";
}
print jrec;
}
}
@yasirbam
Copy link

great script,
One small bug, is it possible to remove the last comma on the last record?

@minkymorgan
Copy link

the last comma problem is corrected by adding two lines to the script, seen here:
minkymorgan / tab2json.awk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment