r - jsonlite 在 json 数组 "parent elements" 末尾添加回车符 returns
r - jsonlite add carriage returns at end of json array "parent elements"
我是在 R 中操作 json 数组的新手。当我使用 R 包 jsonlite 将 json 数组写入 .json 文件时,使用以下命令代码,我在该文件的第一行打印了整个 json 数组(reg 是 data.frame)。
rownames(reg) <- NULL
write(toJSON(reg), file = "test.json")
我希望能够在嵌套层次结构中每个主要 ("parent") 元素的末尾添加回车符 return "\n",因此它看起来像下面这样:
[{"val":"ID1","prop":{"Sub":{"val":"foo"}},
{"val":"ID2","prop":{"Sub":{"val":"bar"}}]
而不是:
[{"val":"ID1","prop":{"Sub":{"val":"foo"}},{"val":"ID2","prop":{"Sub":{"val":"bar"}}]
谁能帮帮我?
注意: 我不想要 "pretty" 布局。我希望每个父 element/all 子属性一行。
这是一个例子data.frame
reg <- data.frame(value=c("ID1", "ID2", "ID3"), properties.Subject.value=c("http://example.org/ID1", "http://example.org/ID2", "http://example.org/ID3"), properties.Subject.properties.value=c("http://example.org/xID1", "http://example.org/xID2", "http://example.org/xID3"))
value properties.Subject.value properties.Subject.properties.value
ID1 http://example.org/ID1 http://example.org/xID1
ID2 http://example.org/ID2 http://example.org/xID2
ID3 http://example.org/ID3 http://example.org/xID3
根据您对换行符的要求,我认为 stream_out
应该可以。
require(jsonlite)
output <- file("test.json")
stream_out(head(mtcars), con = output, verbose = TRUE)
输出
{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16.46,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4"}
{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.875,"qsec":17.02,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4 Wag"}
{"mpg":22.8,"cyl":4,"disp":108,"hp":93,"drat":3.85,"wt":2.32,"qsec":18.61,"vs":1,"am":1,"gear":4,"carb":1,"_row":"Datsun 710"}
{"mpg":21.4,"cyl":6,"disp":258,"hp":110,"drat":3.08,"wt":3.215,"qsec":19.44,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Hornet 4 Drive"}
{"mpg":18.7,"cyl":8,"disp":360,"hp":175,"drat":3.15,"wt":3.44,"qsec":17.02,"vs":0,"am":0,"gear":3,"carb":2,"_row":"Hornet Sportabout"}
{"mpg":18.1,"cyl":6,"disp":225,"hp":105,"drat":2.76,"wt":3.46,"qsec":20.22,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Valiant"}
?stream_out
Because parsing huge JSON strings is difficult and inefficient, JSON streaming is done using lines of minified JSON records, a.k.a. ndjson. This is pretty standard: JSON databases such as dat or MongoDB use the same format to import/export datasets. Note that this means that the total stream combined is not valid JSON itself; only the individual lines are. Also note that because line-breaks are used as separators, prettified JSON is not permitted: the JSON lines must be minified. In this respect, the format is a bit different from fromJSON and toJSON where all lines are part of a single JSON structure with optional line breaks.
我是在 R 中操作 json 数组的新手。当我使用 R 包 jsonlite 将 json 数组写入 .json 文件时,使用以下命令代码,我在该文件的第一行打印了整个 json 数组(reg 是 data.frame)。
rownames(reg) <- NULL
write(toJSON(reg), file = "test.json")
我希望能够在嵌套层次结构中每个主要 ("parent") 元素的末尾添加回车符 return "\n",因此它看起来像下面这样:
[{"val":"ID1","prop":{"Sub":{"val":"foo"}},
{"val":"ID2","prop":{"Sub":{"val":"bar"}}]
而不是:
[{"val":"ID1","prop":{"Sub":{"val":"foo"}},{"val":"ID2","prop":{"Sub":{"val":"bar"}}]
谁能帮帮我?
注意: 我不想要 "pretty" 布局。我希望每个父 element/all 子属性一行。
这是一个例子data.frame
reg <- data.frame(value=c("ID1", "ID2", "ID3"), properties.Subject.value=c("http://example.org/ID1", "http://example.org/ID2", "http://example.org/ID3"), properties.Subject.properties.value=c("http://example.org/xID1", "http://example.org/xID2", "http://example.org/xID3"))
value properties.Subject.value properties.Subject.properties.value ID1 http://example.org/ID1 http://example.org/xID1 ID2 http://example.org/ID2 http://example.org/xID2 ID3 http://example.org/ID3 http://example.org/xID3
根据您对换行符的要求,我认为 stream_out
应该可以。
require(jsonlite)
output <- file("test.json")
stream_out(head(mtcars), con = output, verbose = TRUE)
输出
{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16.46,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4"}
{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.875,"qsec":17.02,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4 Wag"}
{"mpg":22.8,"cyl":4,"disp":108,"hp":93,"drat":3.85,"wt":2.32,"qsec":18.61,"vs":1,"am":1,"gear":4,"carb":1,"_row":"Datsun 710"}
{"mpg":21.4,"cyl":6,"disp":258,"hp":110,"drat":3.08,"wt":3.215,"qsec":19.44,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Hornet 4 Drive"}
{"mpg":18.7,"cyl":8,"disp":360,"hp":175,"drat":3.15,"wt":3.44,"qsec":17.02,"vs":0,"am":0,"gear":3,"carb":2,"_row":"Hornet Sportabout"}
{"mpg":18.1,"cyl":6,"disp":225,"hp":105,"drat":2.76,"wt":3.46,"qsec":20.22,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Valiant"}
?stream_out
Because parsing huge JSON strings is difficult and inefficient, JSON streaming is done using lines of minified JSON records, a.k.a. ndjson. This is pretty standard: JSON databases such as dat or MongoDB use the same format to import/export datasets. Note that this means that the total stream combined is not valid JSON itself; only the individual lines are. Also note that because line-breaks are used as separators, prettified JSON is not permitted: the JSON lines must be minified. In this respect, the format is a bit different from fromJSON and toJSON where all lines are part of a single JSON structure with optional line breaks.