I have a json list that its saved as text, and i'm trying to convert it into a data frame.List looks like that:
`{"posts": {
"data": [
{
"comments": {
"data": [
{
"created_time": "2020-01-25T16:48:03+0000",
"message": "I love all kind of art and paintings. However 19 thousand dollars for a painting is entirely too much!!. ",
"id": "1579832716452874_1373756966159579"
},
{
"created_time": "2020-01-25T15:21:29+0000",
"message": "The wind blows a piece of paper and lands next to your house. You unravel it, \"Hey hope you are having a nice day!\"...",
"id": "1579832716452874_1373704542831488"
}
]
}
}
]
}
}`
and so on. I would like for my data frame to be split in the following columns:
`created_time|message|id`
along with their respective data. I have tried the following command but with no success as i'm getting the same exact output:
` df <- data.frame(matrix(unlist(data), ncol=length(data), byrow = FALSE))`
Also because the data are saved as text, the json packages (rjson,jsonlite) won't work. Any suggestions will be greatly appreciated.
The issue you seemed to be having was with the quotation marks. If you have your JSON object saved as a text file,
{jsonlite}
will automatically escape those characters when reading the file. The defaults forfromJSON
flatten lists into dataframes when possible, which is what you wanted.The only tricky bit is figuring out the
x$posts$data[1]$comments$data[[1]]
. I always do that interactively at the console, occasionally checkingstr()
of the resulting object to see where there are hidden levels of nesting that aren't printing clearly.