Skip to content

Instantly share code, notes, and snippets.

@tisseurdetoile
Created March 14, 2013 13:19
Show Gist options
  • Save tisseurdetoile/5161238 to your computer and use it in GitHub Desktop.
Save tisseurdetoile/5161238 to your computer and use it in GitHub Desktop.
json output fromage sqlite select
# sqlitetojson.awk
# convert SQL SELECT Result from sqlite to json
# usage : sqlite3 -header dbfilename 'SELECT * FROM database' | awk -f sqlitetojson.awk
# nota : -header is mandatory
BEGIN {
FS="|";
nh = 0;
printf "[";
}
NR==1 {
split($0,header);
}
NR>1 {
if (NR > 2) {
print ",";
}
printf "{";
for (i = 1; i <= (NF); i++) {
if (i > 1) {
printf ",";
}
printf "\"%s\":", header[i];
if ( $i ~ /^[-+]?[0-9]*\.?[0-9]+$/ ) {
printf "%s", $i;
} else {
printf "\"%s\"", $i;
}
}
printf "}";
}
END {
print "]";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment