In my current project, I am trying to read data from csv
file and trying to create hierarchical JSON
array based on the data from csv
file in R. Sample data is shown below:
Added data sample data (Reduced the dataset for simplicity):
Country Provider 2 G Data 3 G Data LTE FP0 anfang0 2G 3G FP1 anfang1
ABC A1 n n n fp0 j NA NA NA NA
ABC A2 NA NA NA NA NA j j fp1 n
ABC A3 n n n fp0 j NA NA NA NA
DEF A7 j j j fp0 n j j fp1 n
Understanding of data: n
stands for value is no
, j
stands for value is yes
and NA
stands for value is missing. FP0
and FP1
represent the information about the same provider but in a different area. There are two types of data in a single row i.e.2 G Data, 3 G Data, LTE, FP0, anfang 0
belong to 1 group and 2G, 3G, FP1, anfang 1
belong to other group. If all information is n
i.e. no
then we have to consider corresponding anfang0
or anfang1
value.
The sample output is shown below (based on the above explanation):
{
"ABC": {
"fp0":[
{
"provider": "A1",
"anfrage": "j"
},
{
"provider": "A3",
"anfrage": "j"
}
],
"fp1": [
{
"provider": "A2",
"2G": "j",
"3G": "j"
}
]
},
"DEF": {
"fp1": [
{
"provider": "A7",
"2G": "j",
"3G": "j"
}
],
"fp0": [
{
"provider": "A7",
"2G": "j",
"3G": "j",
"LTE": "j"
}
]
}
}
In the above json
format, for each Country
there should be only single json
block as shown above. So far I tried to follow this link but couldn't find any working solution.
for(i in 1:nrow(data)){
a=c(a,jsonlite::toJSON(list(list('fp0' =
list("provider"=data$Provider[i],"2g"=data$`2 G Data`[i],"3g"=data$`3 G
Data`[i],"LTE"=data$LTE[i]))), pretty = TRUE))
}
toJSON(a, pretty = TRUE, auto_unbox = TRUE)
Kindly let me know in case you need more clarity.