Created
March 14, 2013 13:19
-
-
Save tisseurdetoile/5161238 to your computer and use it in GitHub Desktop.
json output fromage sqlite select
This file contains 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
# 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