Using jsonlite to write an unnamed list from R into a json array
Asked Answered
B

2

6

I'm sending data to a C# developer who wants to use http://json2csharp.com/ to convert my data string into an array of objects. I thought that the jsonlite package would be good for this, but he's not getting the results he wants, and as I examine the produced output, its not quite what I'd expect. My interpretation of the situation was that the output generated by

x <- list( 
  a=list( a=1,b='x',c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
  b=list( a=2,b='y',c=list("Hello","World"), d=as.Date("2014-12-31") ),
  c=list( a=3,b='z',c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x, pretty = FALSE )

was

{"a":{"a":[1],"b":["x"],"c":[["Foo"],["Bar"]],"d":["2015-01-01"]},"b":{"a":[2],"b":["y"],"c":[["Hello"],["World"]],"d":["2014-12-31"]},"c":{"a":[3],"b":["z"],"c":{},"d":["2016-02-29"]}} 

which put all the child element data into lists, while considering the parent named list to be an object of named elements. R doesn't have the same kind of typing that JSON was designed for, so this is probably the intended behaviour of toJSON, but I wanted something more like

[{"a":1,"b":"x","c":["Foo","Bar"],"d":"2015-01-01"},{"a":2,"b":"y","c":["Hello","World"],"d":"2014-12-31"},{"a":3,"b":"z","c":{},"d":"2016-02-29"}] 

How does one write a list in R to a JSON array in jsonlite?

Brainstorm answered 19/8, 2015 at 17:48 Comment(0)
B
12

So it turns out that unnaming the list and setting auto_unbox was what I was looking for. Changing the code to:

x2 <- list( 
  list( a=1,b='x',c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
  list( a=2,b='y',c=list("Hello","World"), d=as.Date("2014-12-31") ),
  list( a=3,b='z',c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x2, auto_unbox = TRUE, pretty = FALSE )

OR

x2 <- unname(x)
toJSON(x2, auto_unbox = TRUE, pretty = FALSE )

resulted in the array of objects that I was looking for:

[{"a":1,"b":"x","c":["Foo","Bar"],"d":"2015-01-01"},{"a":2,"b":"y","c":["Hello","World"],"d":"2014-12-31"},{"a":3,"b":"z","c":{},"d":"2016-02-29"}] 

I couldn't name them, though.

Brainstorm answered 19/8, 2015 at 17:48 Comment(1)
Absolutely brilliant answer!Eleanor
T
1

Using auto_unbox works for me:

x <- list( 
  a=list( a=1,b='x',c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
  b=list( a=2,b='y',c=list("Hello","World"), d=as.Date("2014-12-31") ),
  c=list( a=3,b='z',c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x, auto_unbox=TRUE)
{"a":{"a":1,"b":"x","c":["Foo","Bar"],"d":"2015-01-01"},"b":{"a":2,"b":"y","c":["Hello","World"],"d":"2014-12-31"},"c":{"a":3,"b":"z","c":{},"d":"2016-02-29"}} 

According to the documentation, it is recommended to manually unbox scalars, like so:

x2 <- list( 
  a=list( a=jsonlite::unbox(1),b=jsonlite::unbox('x'),c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
  b=list( a=jsonlite::unbox(2),b=jsonlite::unbox('y'),c=list("Hello","World"), d=as.Date("2014-12-31") ),
  c=list( a=jsonlite::unbox(3),b=jsonlite::unbox('z'),c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x2)
{"a":{"a":1,"b":"x","c":[["Foo"],["Bar"]],"d":["2015-01-01"]},"b":{"a":2,"b":"y","c":[["Hello"],["World"]],"d":["2014-12-31"]},"c":{"a":3,"b":"z","c":{},"d":["2016-02-29"]}} 
Titled answered 24/8, 2022 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.