I am trying to convert a JSON response file using the JSONlite package, but am running into a few issues in the way the data is being presented in a data frame.
Using this code:
library(dplyr)
library(jsonlite)
json_data <- fromJSON("File Path")
df <-data.frame(unlist(json_data))
JSON Response looks like:
{
"totalPages": 1,
"firstPage": true,
"lastPage": true,
"numberOfElements": 1757,
"number": 0,
"totalElements": 1757,
"columns": {
"dimension": {
"id": "variables/evar4",
"type": "string"
},
"columnIds": [
"1",
"2",
"3",
"4"
]
},
"rows": [
{
"itemId": "0",
"value": "Unspecified",
"data": [
220,
1.0002442201269182,
0.29499089253187616,
2.890909090909091
]
},
{
"itemId": "118293442",
"value": "PNIwTjWWjphkqfu",
"data": [
5,
1.0002442201269182,
57.879999999999995,
30.8
]
},
{
"itemId": "1810135314",
"value": "PNIFBOIKLplumdb",
"data": [
3,
1.0002442201269182,
1243.0277777777778,
545.3333333333334
]
}
]
}
I receive a table df that has columns and rows all in one list. Is there a way to have the column ids going across the top, with the corresponding rowIDs matched to the proper column?
So that the data frame looks like:
1 2 3 4
Unspecified 220 1.00 0.294 2.89
PNIwTjWWjphkqfu 5 1.00 57.87 30.8
PNIFBOIKLplumdb 3 1.00 1243.0 545.33
Any help on this would be greatly appreciated