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