Skip to content
Advertisement

Importing JSON data from SQL DB to an R dataframe

I would like to know whether there is a way of importing JSON data from a MySQL DB to an R dataframe.

I have a table like this:

I would like to get the columns ‘id’ and ‘json’. I am using RMySQL package for getting the data from the db to an R dataframe but this gives me only the column ‘id’, the column ‘json’ contains only NAs in each row.

Is there any way how to import/load the data and get the json column displayed? And possibly to extract the “sensor” part of the json values?

The result would be a dataframe (df) like this:

Or with with the extracted value:

Thank you very much for any suggestions.

Advertisement

Answer

Using unnest_wider from tidyr

Result:

If you want to only retrieve data from the last 24 hours (as the OP requested) change the tbl(con, 'table_name') statement to:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement