I am writing a function to convert JSON file to SQLite file by representing it in a table, but I am having a little trouble with indexing objects without keys. For example, I am converting this JSON file but not sure how to index it correctly.
[ [ "A1", "A2", "A3" ],
[ 1.1, 2.2, 3.3],
[ 4.4, 5.5, 6.6] ]
So far this is all I have
library(rjson)
library(sqldf)
library(RSQLite)
convert = function (infile,outfile,name = "test"){
data = fromJSON(file = infile)
cols = length(data$header) #data[1]
rows = length(data$data) #data[2]
temp = matrix(NA, rows, cols) #create an empty dataframe and populate it
for (i in 1:rows){
for (j in 1:cols){
temp[i,j] = ((data$data)[[i]])[[j]]
}
}
#Save data into the dataframe
temp = as.data.frame(temp)
colnames(temp) = data$header
#Save to sqlite
db = dbConnect(SQLite(), dbname = outfile)
dbWriteTable(conn = db, name = name, value = result, row.names = FALSE, overwrite = TRUE)
}
infile is the JSON file containing a list. The first item of the list is a list of strings specifying column names. Subsequent items of the list are lists of numerical values. outfile is an SQLite database with a table representing the data in infile with 3 columns A1 A2 A3.