How to combine multiple JSON files into a single file in R

后端 未结 1 1505
天命终不由人
天命终不由人 2021-01-13 06:40

i have three JSON files

  • json1 contains [[1,5],[5,7],[8,10]]
  • json2 contains [[5,6],[4,5],[5,8]]
相关标签:
1条回答
  • 2021-01-13 07:08

    If you are using the rjson package, then you need to concatenate them into a list:

    library(rjson)
    json1 <- fromJSON(file = "json1")
    json2 <- fromJSON(file = "json2")
    json3 <- fromJSON(file = "json3")
    jsonl <- list(json1, json2, json3)
    jsonc <- toJSON(jsonc)
    jsonc
    [1] "[[[1,5],[5,7],[8,10]],[[5,6],[4,5],[5,8]],[[4,7],[3,4],[4,8]]]"
    write(jsonc, file = "jsonc")
    

    If you have many files, you can put them in a vector and use lapply to save some typing:

    files <- c("json1", "json2", "json3")
    jsonl <- lapply(files, function(f) fromJSON(file = f))
    jsonc <- toJSON(jsonl)
    write(jsonc, file = "jsonc")
    
    0 讨论(0)
提交回复
热议问题