0

I am new to manipulating json arrays in R. When I write a json array using R package jsonlite to a .json file using the below code, I get the entire json array printed on the first line of that file (reg is a data.frame).

rownames(reg) <- NULL
write(toJSON(reg), file = "test.json")

I would like to be able to add a carriage return "\n" at the end of each major ("parent") element in the nested hierarchy, so instead it looks like the below:

[{"val":"ID1","prop":{"Sub":{"val":"foo"}},
{"val":"ID2","prop":{"Sub":{"val":"bar"}}]

instead of:

[{"val":"ID1","prop":{"Sub":{"val":"foo"}},{"val":"ID2","prop":{"Sub":{"val":"bar"}}]

Can anyone help me?

Note: I don't want the "pretty" layout. I want one line per parent element/all children properties.

Here is an example 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
lrthistlethwaite
  • 494
  • 2
  • 6
  • 23

1 Answers1

0

As per your requirements of new line I think stream_out should work.

require(jsonlite)
output <- file("test.json")
stream_out(head(mtcars), con = output, verbose = TRUE)

output

{"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.

user5249203
  • 4,436
  • 1
  • 19
  • 45