How to combine data into a pre-existing JSON structure in R?

The first poster, a long time. To be gentle. Moderate user R. I am sure that there is a better, functional way to do what I need, but I felt like I was investigating death without understanding.

I am trying to combine a dataset with an existing JSON structure. Where is one row of entries per JSON structure for many consecutive JSON requests.

I load the data set into data, which is 13 variables, and change the column headers to match the way they appear in the JSON structure.

library(jsonlite)
#### Map Column headers to their respective names in the JSON Structure
colnames(data) <- c("default.A",
                    "default.B",
                    "default.C",
                    "items.A",
                    "items.B.1",
                    "items.B.2",
                    "items.B.3",
                    "items.B.4",
)

Create an empty JSON structure. This is the format for which you need to handle JSON requests. Simple nested structure.

sample <- '{
      "default": {
           "A": "",
           "B": "",
           "C": "",
            },
      "items": [{
           "A": "",
           "B": {
                "1": "",
                "2": "",
                "3": "",
                "4": "",
                     }
                }]
           }'

jsonstructure <- fromJSON(sample)

set everything as df. combine them. Fill in NA with spaces

x <- as.data.frame(data)
y <- as.data.frame(jsonstructure)
Z <- merge(x, y, all = TRUE)
Z[is.na(Z)] <- ""

Convert to JSON

jsonZ <- toJSON(unname(split(Z, 1:nrow(Z))), pretty=TRUE)
cat(jsonZ)

Current output that does not match

[
  [
    {
   "default.A": "",
      "default.B": "1234567890",
      "default.C": "",
      "items.A": "1234567890",
      "items.B.1": "1234",
      "items.B.2": "1234",
      "items.B.3": "1234",
      "items.B.4": "1234",
    }
  ],
  [
    {
   "default.A": "",
      "default.B": "0987654321",
      "default.C": "",
      "items.A": "0987654321",
      "items.B.1": "4321",
      "items.B.2": "4321",
      "items.B.3": "4321",
      "items.B.4": "4321",
    }
  ]
]
+4
2

- , . . .

library(jsonlite)

#data.frame with data - you have probably more than 2 rows
data=data.frame(rbind(t(c(NA,1234567890,NA,1234567890,1234,1234,1234,1234)),
                      t(c(1,NA,2,3,1,1000,NA,1234))))

cn=c("default.A",
      "default.B",
      "default.C",
      "items.A",
      "items.B.1",
      "items.B.2",
      "items.B.3",
      "items.B.4")

colnames(data)=cn

#assuming that "." represents structure
mapping=strsplit(cn,"\\.")

#template JSON
jsonstructure <- fromJSON('{"default": {"A": "","B": "","C": ""},
                          "items": [{"A": "",
                                     "B": {"1": "","2": "","3": "","4": ""}}]}')

#now loop through all rows in your data.frame and store them in JSON format
#this will give you a list with JSON objects (i.e., a list of lists)
json_list=lapply(split(data,1:nrow(data)),function(data_row) {
  for (i in seq_along(mapping)) jsonstructure[[mapping[[i]]]]<-data_row[,cn[i]]
  jsonstructure
})

:

toJSON(json_list[[2]],pretty = TRUE, auto_unbox=TRUE)
#{
#  "default": {
#    "A": 1,
#    "B": "NA",
#    "C": 2
#  },
#  "items": [
#    {
#      "A": 3,
#      "B": {
#        "1": 1,
#        "2": 1000,
#        "4": 1234
#      }
#    }
#  ]
#} 

. , [:

[[ , , p, alist [[i]] alist [[i1]]... [[ip]], , .

+1

jsonlite, rjson

library(rjson)

value = c("", "1234690","")
names(value) = c("A","B","C")


value2 = c("","0987654321","","0987654321")
names(value2) = c("1","2","3","4") 

test <- toJSON(list( "default" = value, "items" =  list(c("A" = "", "B" = list(value2))) ))
cat(test)
writeLines(test, "test.json")
0

Source: https://habr.com/ru/post/1662995/


All Articles