ropensci / jqr

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

JSON field missing #89

Closed oggioniale closed 10 months ago

oggioniale commented 10 months ago
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 14.0

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Rome
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_2.0.3

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.1-3 rstudioapi_0.15.0  jsonlite_1.8.7     wk_0.7.3           rdflib_0.2.7       farver_2.1.1      
  [7] rmarkdown_2.23     vctrs_0.6.4        RCurl_1.98-1.12    base64enc_0.1-3    terra_1.7-29       htmltools_0.5.5   
 [13] leafsync_0.1.0     curl_5.1.0         raster_3.6-20      s2_1.1.4           KernSmooth_2.23-20 keyring_1.3.1     
 [19] htmlwidgets_1.6.2  plyr_1.8.9         httr2_0.2.2        zoo_1.8-12         lubridate_1.9.2    stars_0.6-1       
 [25] emld_0.5.1         uuid_1.1-0         whisker_0.4.1      lifecycle_1.0.3    iterators_1.0.14   pkgconfig_2.0.3   
 [31] rebird_1.3.0       R6_2.5.1           fastmap_1.1.1      rbibutils_2.2.13   digest_0.6.33      dtplyr_1.3.1      
 [37] colorspace_2.1-0   leafem_0.2.0       crosstalk_1.2.0    rosm_0.2.6         lwgeom_0.2-13      conditionz_0.1.0  
 [43] bold_1.3.0         fansi_1.0.5        timechange_0.2.0   httr_1.4.7         polyclip_1.10-4    abind_1.4-5       
 [49] compiler_4.3.0     proxy_0.4-27       bit64_4.0.5        withr_2.5.2        qrcode_0.2.1       DBI_1.1.3         
 [55] zen4R_0.9.9000     ggforce_0.4.1      Rttf2pt1_1.3.12    maps_3.4.1         MASS_7.3-58.4      rappdirs_0.3.3    
 [61] tmaptools_3.1-1    leaflet_2.1.2      classInt_0.4-9     oai_0.4.0          tools_4.3.0        units_0.8-3       
 [67] ape_5.7-1          zip_2.3.0          tmap_3.3-3         extrafontdb_1.0    rgbif_3.7.7        glue_1.6.2        
 [73] nlme_3.1-162       atom4R_0.3-3       grid_4.3.0         sf_1.0-14          generics_0.1.3     xslt_1.4.4        
 [79] gtable_0.3.3       countrycode_1.4.0  tzdb_0.4.0         class_7.3-21       tidyr_1.3.0        data.table_1.14.8 
 [85] hms_1.1.3          sp_1.6-0           xml2_1.3.5.9000    utf8_1.2.4         EML_2.0.6.1        foreach_1.5.2     
 [91] pillar_1.9.0       stringr_1.5.0      vroom_1.6.4        redland_1.0.17-17  ridigbio_0.3.6     dplyr_1.1.3       
 [97] tweenr_2.0.2       lattice_0.21-8     bit_4.0.5          tidyselect_1.2.0   knitr_1.45         gridExtra_2.3     
[103] V8_4.3.0           waffle_0.7.0       worrms_0.4.3       crul_1.4.0         xfun_0.41          spocc_1.2.2       
[109] taxize_0.9.100     stringi_1.7.12     lazyeval_0.2.2     yaml_2.3.7         evaluate_0.23      codetools_0.2-19  
[115] httpcode_0.3.0     extrafont_0.19     tibble_3.2.1       cli_3.6.1          Rdpack_2.4         roxygen2_7.2.3    
[121] munsell_0.5.0      rvertnet_0.8.2     jqr_1.2.3          dichromat_2.0-0.1  Rcpp_1.0.11        png_0.1-8         
[127] XML_3.99-0.15      ReLTER_2.1.2       parallel_4.3.0     jsonld_2.2         ggplot2_3.4.2      readr_2.1.4       
[133] assertthat_0.2.1   bitops_1.0-7       viridisLite_0.4.2  scales_1.2.1       e1071_1.7-13       purrr_1.0.2       
[139] crayon_1.5.2       rlang_1.1.1

I would like to get a result even if the field is missing. In this example x contains the resource_type field, in the case of y the field is missing. How do I make a query that is valid in both cases and returns both publication_state and type?

x <- 'https://b2share.eudat.eu/api/records/ab26962723df42ed9f575150d3e38c16'
jqr::jq(
  url(x),
  as.character(
    "{
    publication_state: .metadata.publication_state,
    type: .metadata.resource_types[].resource_type
    }"
  )
)
{
    "publication_state": "published",
    "type": "dataset"
}

y <- 'https://b2share.eudat.eu/api/records/a572efec373446169949f7769f829fc4'
jqr::jq(
  url(y),
  as.character(
    "{
    publication_state: .metadata.publication_state,
    type: .metadata.resource_types[].resource_type
    }"
  )
)
[

]
oggioniale commented 10 months ago

solved with this query:

publication_state: .metadata.publication_state,
type: .metadata | [if (has("resource_types")) then .resource_types[].resource_type_general else "no_resource_types_declared" end][]

so the full example for JSON without type is:

y <- 'https://b2share.eudat.eu/api/records/a572efec373446169949f7769f829fc4'
jqr::jq(
  url(y),
  as.character(
    "{
      publication_state: .metadata.publication_state,
      type: .metadata | [if (has("resource_types")) then .resource_types[].resource_type_general else "no_resource_types_declared" end][]
    }"
  )
)
[
    "publication_state": "published",
    "type": "no_resource_types_declared"
]