jqr icon indicating copy to clipboard operation
jqr copied to clipboard

Allow users to specify jq options

Open sckott opened this issue 9 years ago • 29 comments

see the flags under https://stedolan.github.io/jq/manual/#Invokingjq section - @richfitz do you know if this is possible?

Some of them I think only make sense on the command line outside of R, but I think some could be used, e.g., --sort-keys

sckott avatar Jan 11 '16 17:01 sckott

They could be any named arguments in the ....

smbache avatar Jan 11 '16 18:01 smbache

hadn't tried it yet, just tried now, nothing works yet, an eg?

sckott avatar Jan 11 '16 18:01 sckott

Why would it work yet? :)

smbache avatar Jan 11 '16 18:01 smbache

im confused

sckott avatar Jan 11 '16 18:01 sckott

Me too :)

My point was only the interface; that ... in the jq call, can be named and unnamed. The unnamed are used for the query itself, and the named should be passed on as options,

e.g.

jso %>% jq(".key .subkey", sort_keys = TRUE)

I guess the =TRUE part is a bit redundant here, alternatively something like

jso %>% jq(".key .subkey", .flags = list("--sort-keys"))

smbache avatar Jan 11 '16 18:01 smbache

I guess you need to actually allow for the flasgs to passed along to the actual jq call under the hood

smbache avatar Jan 11 '16 18:01 smbache

I can make the changes to the jq functions, provided you make it possible to pass them on.

smbache avatar Jan 11 '16 19:01 smbache

Was confused as I thought you were suggesting it should work now. But I figured it shouldn't.

Right, we need to allow flags to be used. I imagine there's a way to hook into options via the C API, any thoughts @richfitz

We could use a separate function like:

json %>% jq(".key .subkey") %>% flags("--sort-keys")

I don't think this needs to happen before cran push, but perhaps it does if it means a change to how jq() works

sckott avatar Jan 11 '16 19:01 sckott

Right. I guess the dsl could use a flags function. The low-level jq call could pass it as a named argument. In both cases it wouldn't be a hassle to introduce later, as adding flags after ... would not break anything (removing it again, would ;)).

So, e.g.

(jso
 %>% something
 %>% something_else
 %>% flags("--sort-keys")
)

# or
jso %>% jq("something something_else", flags = list("--sort-keys"))

I think it might get messy if we need to have low-level jq fiddled into the auto-execute story.

smbache avatar Jan 11 '16 19:01 smbache

I vote for a separate function. Let's wait to see what rich says about hooking into the C API

sckott avatar Jan 11 '16 19:01 sckott

Will need a bit of adjustments, I think, not that's impossible; but we need to assess whether it's worth it.. also it's not as low-level-ish ;)

smbache avatar Jan 11 '16 20:01 smbache

Sure, I'll have a look - perhaps this week.

Looks like sort-keys becomes the SORTED_OUTPUT option becomes JV_PRINT_SORTED and that's what we want to target. The other options in there are PRINT_PRETTY, PRINT_ASCII and PRINT_COLOUR (woohoo English spelling).

JV_PRINT_SORTED is consumed by jv_dump_term, passed in by a flags argument, which is passed from jv_dump_string which we use!

richfitz avatar Jan 11 '16 22:01 richfitz

(bear in mind though that the only connection between the command line interface and libjq is the implementation details in jq's main.c)

richfitz avatar Jan 11 '16 22:01 richfitz

OK, so that's done at the low level:

str <- "{\"z\": 7, \"b\": 10}"
message(jqr(str2, ".", jq_flags(sorted=TRUE,pretty=TRUE,color=TRUE)))
# {
#   "b": 10,
#   "z": 7
# }

in colour on mac terminal (not Rstudio until they get ANSI colouring). I'm not sure how you want to mix that in with the DSL stuff; perhaps it goes in query_from_dots, perhaps it goes through things like do? It's late here so I don't want to break it!

richfitz avatar Jan 11 '16 23:01 richfitz

Thanks @richfitz

sckott avatar Jan 12 '16 18:01 sckott

@richfitz Any thoughts on how to update flags methods to implement the remainder of the jq flags? I'm not quite sure from looking at main.c how to do the remainder of the flags, main.c has https://github.com/ropensci/jqr/blob/master/src/jq/main.c#L111-L127 but those are a subset of all the flags. maybe some flags we can't use in R

sckott avatar Aug 11 '17 22:08 sckott

Those flags are used only within src/jq/main.c and are not part of the libjq interface I think. We don't compile anything from that file into the library

richfitz avatar Aug 14 '17 08:08 richfitz

Ah - how were you able to do the 4 flags you implemented then?

sckott avatar Aug 14 '17 17:08 sckott

Those are (I think) just flags to jv_dump_string, coming through here: https://github.com/ropensci/jqr/blob/e92060a6729fb44a5d9624a253811237bb2c234d/src/jq/jv.h#L188-L199

richfitz avatar Aug 14 '17 19:08 richfitz

thanks , hmm, doesn't seem like all the flags in the docs are in that file

sckott avatar Aug 14 '17 20:08 sckott

I think different entry points in jq have different sets of flags. Or I'm confused about what part of the docs you are seeing different sets of flags, perhaps 🎏 🇨🇦

richfitz avatar Aug 15 '17 19:08 richfitz

the flags listed here https://stedolan.github.io/jq/manual/#Invokingjq

sckott avatar Aug 15 '17 19:08 sckott

yeah, I think that those are all doing things in src/jq/main.c and not part of the library; we'd have to reimplement that in a library style to do that in the package

richfitz avatar Aug 15 '17 19:08 richfitz

okay, blegh

sckott avatar Aug 15 '17 19:08 sckott

An example of an option that'd be nice is --slurp, this would make merging arrays a bit easier, like so:

x <- c('[1,2,3]','[4,5,6]')
## want: [1,2,3,4,5,6]
jq(x, 'flatten', .options = list('--slurp'))

Is there another easy way to combine (an arbitrarily-long collection of) arrays without the slurp option?

mmuurr avatar Aug 15 '18 00:08 mmuurr

agree, --slurp would be nice. I don't know of another way to combine two arrays.

sckott avatar Aug 15 '18 01:08 sckott

thanks for the upvote on this, will try to get it sorted

sckott avatar Aug 15 '18 01:08 sckott

The easiest way (that I've come up with) to mimic the --slurp behavior if you have a vector of JSON array strings, is something like:

jq_slurp <- function(x) sprintf("[%s]", paste0(x, collapse = ","))
x <- c('[1,2,3]','[4,5,6]')
x %>% jq_slurp %>% jq('flatten')

This is a language-specific (i.e. R-specific) solution, however, and one of the advantages of jq bindings is the common filter string language shared across different platforms, languages, etc.

Though worth mentioning that I think jqr is already great and has drastically simplified my parsing/manipulation of JSON documents, so thanks!

mmuurr avatar Aug 15 '18 03:08 mmuurr

right, there is that solution, glad at least something works. Glad jqr is already helping you out! hopefully we'll get this sorted soon

sckott avatar Aug 16 '18 16:08 sckott