Simple JSON
matching.
devtools::install_github('chiefBiiko/jsonmatch')
jsonmatch::jsonmatch(json, pattern, auto_unbox=FALSE, strict=TRUE)
json
character. In-memory JSON
string or filename. requiredpattern
character. Subset pattern. requiredauto_unbox
logical. Unbox JSON
? Default: FALSE
. optionalstrict
logical. Only allow valid JSON
? Default: TRUE
. optionalReturn character, json. JSON
subset.
pattern
allows matching keys of JSON
arrays and objects. Its syntax rules are simple:
[0]
or [1,3]
or [2:]
or [2:3]
.key
or .k*y
or .*y
or .k*
or .*
.keyA[0],.keyB[0]
Note that a colon within an array subset pattern is only valid if it is trailing, indicating to select all array elements with an index in the range of [low:]
to length(array) - 1
aka the end of the array. The wildcard character in an object subset pattern matches any alphanumeric character sequence. Wildcard matching cannot be used if object keys contain non-alphanumeric characters [^[:alnum:]]
.
For the rationale of auto_unbox
check out boxjson
. If strict
is TRUE
and jsonlite::validate(json)
evaluates to FALSE
an error is thrown.
# some JSON
some.json <- jsonlite::toJSON(list(list(name='herman', gang='almans'),
list(name='habibo', gang=c('haji', '419'))))
# peek at it
cat('some JSON:\n', some.json, sep='')
some JSON:
[{"name":["herman"],"gang":["almans"]},{"name":["habibo"],"gang":["haji","419"]}]
# who u with
gangs <- jsonmatch::jsonmatch(some.json, '[0:].gang')
cat('gangs:\n', gangs, sep='')
gangs:
[["almans"],["haji","419"]]
MIT
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.