ropensci / jqr

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

jq() paste bare numbers together #79

Open mgirlich opened 4 years ago

mgirlich commented 4 years ago

The following code returns "12" instead of "1" and "2" separately

as.character(jqr::jq(c("1", "2")))
#> [1] "12"
# expected
# [1] "1" "2"

Created on 2020-01-24 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.1 (2019-07-05) #> os macOS Catalina 10.15.2 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz UTC #> date 2020-01-24 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) #> backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0) #> callr 3.4.0 2019-12-09 [1] CRAN (R 3.6.0) #> cli 2.0.1 2020-01-08 [1] CRAN (R 3.6.1) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0) #> devtools 2.2.1 2019-09-24 [1] CRAN (R 3.6.0) #> digest 0.6.23 2019-11-23 [1] CRAN (R 3.6.0) #> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.1) #> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0) #> glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) #> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0) #> jqr 1.1.0 2018-10-22 [1] CRAN (R 3.6.0) #> jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.0) #> knitr 1.27 2020-01-16 [1] CRAN (R 3.6.0) #> lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0) #> pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.0) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.1) #> processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0) #> ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0) #> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.0) #> Rcpp 1.0.3 2019-11-08 [1] CRAN (R 3.6.0) #> remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.0) #> rlang 0.4.2.9001 2020-01-22 [1] Github (r-lib/rlang@d6194d9) #> rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) #> stringi 1.4.5 2020-01-11 [1] CRAN (R 3.6.1) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> testthat 2.3.1 2019-12-01 [1] CRAN (R 3.6.0) #> usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.0) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0) #> xfun 0.12 2020-01-13 [1] CRAN (R 3.6.1) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```
sckott commented 4 years ago

thanks for this @mgirlich

jq expects JSON as the input as the documentation states, so this works as expected i think

jq(jsonlite::toJSON(c("1", "2")))
#> [
#>     "1",
#>     "2"
#> ]
mgirlich commented 4 years ago

bare numbers are also valid json (see standard and try out at jsonlint). My workflow is something like in the following code

x <- jqr::jq(c('{"a": 1}', '{"a": 2}'), '.a')
x
#> [
#>     1,
#>     2
#> ]
jqr::jq(x)
#> 12

Created on 2020-01-27 by the reprex package (v0.3.0)

sckott commented 4 years ago

@jeroen any thoughts on this issue. seems that it shouldn't combine the numbers together

echo '"1" "2"' | jq .
# "1"
# "2"
sckott commented 3 years ago

@mgirlich Sorry about the delay. We have limited bandwidth to work on this. Fastest way is if you have submit a PR with a fix/change

stla commented 3 years ago

Hello,

I encountered a similar strange behavior with arrays of numbers:

> vv
[
    102,
    123
]
> kk
[
    "2019-01-01T00:00:14",
    "2019-01-01T00:01:15"
]
> paste0(kk) # ok
[1] "[\"2019-01-01T00:00:14\",\"2019-01-01T00:01:15\"]"
> paste0(vv) # weird
[1] "102" "123"
> vv %>% tostring() # very weird
"102123"
> kk %>% tostring() # ok
"[\"2019-01-01T00:00:14\",\"2019-01-01T00:01:15\"]"
sckott commented 3 years ago

thanks @stla but the above eg is not reproducible. what are vv and kk in reproducible code

stla commented 3 years ago

Ah yes, sorry.

str <- '{"2019-01-01T00:00:14":102,"2019-01-01T00:01:15":123}'
kk <- str %>% keys()
vv <- str %>% index()
sckott commented 3 years ago

that is weird. if you do it all in one go it turns out as expected i think:

str <- '{"2019-01-01T00:00:14":102,"2019-01-01T00:01:15":123}'
w <- str %>% index() %>% tostring()
w
#> [
#>     "102",
#>     "123"
#> ]

can you do it that way?