ropensci / jqr

R interface to jq
https://docs.ropensci.org/jqr
Other
143 stars 13 forks source link

Process returned elements #2

Closed sckott closed 9 years ago

sckott commented 9 years ago

The returned output from jq may not be proper json, but a sort of vector of json strings.

str <- '[
  {
    "foo": 1,
    "bar": 2
  },
  {
    "foo": 3,
    "bar": 4
  },
  {
    "foo": 5,
    "bar": 6
  }
]'

jqr(str, ".[] | {name: .foo}")
[1] "{\"name\":1}" "{\"name\":3}" "{\"name\":5}"

Process these by default I imagine, so something like

jqr_proc <- function(json, program, ...) {
  lapply(jqr(json, program), jsonlite::fromJSON, ...)
}
jqr_proc(str, ".[] | {name: .foo}")
[[1]]
[[1]]$name
[1] 1

[[2]]
[[2]]$name
[1] 3

[[3]]
[[3]]$name
[1] 5
richfitz commented 9 years ago

Yeah - see the note just below jqr_conv in tests/testthat/test-spec.R - getting nice semantics (without sappy-like pitfalls here) is going to be hard; it's not clear without running the jq program if the expectation is a list back or a single element.

sckott commented 9 years ago

Seems like we shouldn't worry about this too much. Users can coerce to lists if they want.