Skip to content

Instantly share code, notes, and snippets.

@cboettig
Last active May 18, 2018 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cboettig/e8a0947ced5224cd468af941dda002c6 to your computer and use it in GitHub Desktop.
Save cboettig/e8a0947ced5224cd468af941dda002c6 to your computer and use it in GitHub Desktop.
{
"stuff": {
"buried": {
"deep": [
{
"goodstuff": "here",
"name": "Bob Rudis",
"secret_power": 5
},
{
"goodstuff": "here",
"name": "Amanda Dobbyn",
"secret_power": 4,
"more_nested_stuff": {
"a": 1,
"b": 2
}
}
],
"alsodeep": 2342423234,
"deeper": {
"foo": [
{
"goodstuff": 5,
"name": "barb"
},
{
"goodstuff": "here",
"name": "scott"
}
]
}
}
}}
blob <- jsonlite::fromJSON("ex.json", simplifyVector=FALSE)
jqr::jq(x, 'recurse(.[]?) | objects | select(has("goodstuff"))')
devtools::install_github("ropensci/rdflib")
library(rdflib)
## experimental functions for rdflib package
source(system.file("examples/as_rdf.R", package="rdflib"))
blob <- as_rdf(jsonlite::fromJSON(x, simplifyVector=FALSE), vocab = "x:")
q <- '
PREFIX x: <x:>
SELECT ?name, ?power ?b ?id
WHERE {
?object x:goodstuff "here" .
?object x:name ?name .
OPTIONAL { ?object x:secret_power ?power } .
OPTIONAL { ?object x:more_nested_stuff ?y .
?y x:b ?b} .
OPTIONAL {
?parent x:deep ?object .
?parent x:alsodeep ?id
}
}'
rdf_query(blob, q)
# A tibble: 3 x 4
  name          power     b          id
  <chr>         <int> <int>       <dbl>
1 scott            NA    NA         NA 
2 Amanda Dobbyn     4     2 2342423234.
3 Bob Rudis         5    NA 2342423234.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment