I have a url which updates periodically with some JSON data which I would like to convert to a datatable in R with the following code
library(jsonlite)
fromJSON("http://...")
however this is not working and I think it is due to the way the JSON is structured. To my understanding the file is currently structured as following.
{"h1":[{"h2":[{"Name":"Column1Header","Value":"Row1Column1Value"},{"Name":"Column2Header","Value":"Row1Column2Value"}]},{"h2":[{"Name":"Column1Header","Value":"Row2Column1Value"},{"Name":"Column2Header","Value":"Row2Column2Value"}]}]}
I think if I could read in the url as a long string and manipulate it to something like what is below and call the function
fromJSON()
I will be able to obtain the data.table I want.
[{"Column1Header":"Row1Column1Value","Column2Header":"Row1Column2Value"},{"Column1Header":"Row2Column1Value","Column2Header":"Row2Column2Value"}]
Any idea how I can achieve this? My attempt to solve this involved using the readLines()
function and using gsub()
to replace the bits I don't need. However, readLines
is placing "\" all through the data which I am having all sorts of trouble removing and even if I get past that my gsub
approach wouldn't be very robust.
Any help would be much appreciated as reading the file as it is now isn't letting me get into the level of detail I require to extract the "Name,Value" pairs which I require to build my data.table
.
p.s. Something tells me that for some reason the original JSON file is transposed as the column names don't necessarily comply with the export systems naming convention.