If I have some sample data, how do I put it into SQLite (preferably fully automated)?
{"uri":"/","user_agent":"example1"}
{"uri":"/foobar","user_agent":"example1"}
{"uri":"/","user_agent":"example2"}
{"uri":"/foobar","user_agent":"example3"}
If I have some sample data, how do I put it into SQLite (preferably fully automated)?
{"uri":"/","user_agent":"example1"}
{"uri":"/foobar","user_agent":"example1"}
{"uri":"/","user_agent":"example2"}
{"uri":"/foobar","user_agent":"example3"}
I found the easiest way to do this is by using jq and CSV as an intermediary format.
Edit: as pointed out (thanks @Leo), the original question did show newline delimited JSON objects, which each on their own conform to rfc4627, but not all together in that format. jq can handle a single JSON array of objects much the same way though by preprocessing the file using
jq '.[]' <input.json >preprocessed.json
. If you happen to be dealing with JSON text sequences (rfc7464) luckily jq has got your back too with the--seq
parameter.Getting the CSV
First write your data to a file. I will assume data.json here.
Then construct the header using
jq
:The
head -1
is because we only want one line.jq
's-r
makes the output a plain string instead of a JSON-String wrapping the CSV. We then call the internal functionkeys
to get the keys of the input as an array. This we send to the@csv
formatter which outputs us a single string with the headers in quoted CSV format.We then need to construct the data.
We now take the whole input and deconstruct the associative array (map) using
.[]
and then put it back into a simple array[…]
. This basically converts our dictionary to an array of keys. Sent to the@csv
formatter, we again get some CSV.Putting it all together we get a single one-liner in the form of:
If you need to convert the data on the fly, i.e. without a file, try this:
Loading it into SQLite
Open an SQLite database:
Now in the interactive shell do the following (assuming you wrote the CSV to data.csv and want it in a table called
my_table
):Now close the shell and open it again for a clean environment. You can now easily
SELECT
from the database and do whatever you want to.Putting it all together
Have an asciinema recording right there:
sqlitebiter appears to provide a python solution:
A CLI tool to convert CSV/Excel/HTML/JSON/LTSV/Markdown/SQLite/TSV/Google-Sheets to a SQLite database file. http://sqlitebiter.rtfd.io/
docs: http://sqlitebiter.readthedocs.io/en/latest/
project: https://github.com/thombashi/sqlitebiter