i have three JSON files
json1
contains [[1,5],[5,7],[8,10]]
json2
contains [[5,6],[4,5],[5,8]]
json3
contains [[4,7],[3,4],[4,8]]
I want to merge them into one single file jsonmerge
:
[[[1,5],[5,7],[8,10]],[[5,6],[4,5],[5,8]],[[4,7],[3,4],[4,8]]]
I tried concatenate but it gave results in this format
[[5,6],[4,5],[5,8]],
[[5,6],[4,5],[5,8]],
[[4,7],[3,4],[4,8]]
Any suggestions?
thanks in advance.
Step 1: Load the json files with the help of pandas dataframe. Step 2 : Concatenate the dataframes into one dataframe. Step 3: Convert the concatenated dataframe into CSV file.
The file is invalid if it contains more than one JSON object. When you try to load and parse a JSON file with multiple JSON objects, each line contains valid JSON, but as a whole, it is not a valid JSON as there is no top-level list or object definition.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With