1

I want to format sqlite output in json format from the command line. Currently I have .csv output that looks like this:

label1,value1
label2,value2
label3,value3
...

Now I'd like to have it formatted like this:

{'label1' : 'value1',  'label2': 'value2', ... }

Thanks!

2

I tested this with GNU awk and sed... not sure if it will with other versions.

awk -F, '{printf "'\''%s'\'' : '\''%s'\'', ", $1, $2} END {print "}" }' input.csv | sed -e 's/^/{/' -e 's/, }/}/' > output.json
2

Valid JSON uses double quotes so:

awk -F, 'BEGIN{print "{"} {if (notfirst) {print ","}; print "\"" $1 "\":\"" $2 "\""; notfirst=1} END {print "\"\":\"\"}" }' input.csv

Also simpler not using sed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.