0

I have an SQL database where I want to select a column of JSONfiles and convert it to a readable R dataframe. I succeed in creating a dataframe of the JSONfiles from my database, but when I try to read them in with the fromJSON function it doesn't read all the JSONfiles.

the data$products_json column is a column with in every row a JSON file.

rs = dbSendQuery(mydb, "SELECT products_json FROM orders")
data = fetch(rs, n=-1)

library(rjson)

jfile <- fromJSON(data$products_json)
1
  • Please show sample of JSON as it depends on structure to migrate the nested, tree type into two dimensions of dataframe. Also, please mention your RDBMS (SQL Server, Postgres, etc.) as methods may be available to query JSON into tabular format. Since, SQL is a language, SQL Database is akin to saying English book. Commented Jul 7, 2019 at 14:03

1 Answer 1

1

You can try to collapse all JSON docs into one big JSON array. This will result in a structure that jsonlite can handle. Jsonlite also has a fromJSON function that makes it very easy to transform a JSON array into an R dataframe.

In your case this would be:

library(jsonlite)

# This will collapse the products_json into one string that represents a json_array
partial_array <- paste(data$products_json,  collapse = ', ')
json_array <- paste('[', partial_array, ']')

jfile <- jsonlite::fromJSON(json_array)

Where products_json is a column of JSON docs in data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.