j_data_type {rjsoncons} | R Documentation |
Detect JSON and NDJSON data and path types
Description
j_data_type()
uses simple rules to determine whether
'data' is JSON, NDJSON, file, url, or R.
j_path_type()
uses simple rules to identify
whether path
is a JSONpointer, JSONpath, or JMESpath
expression.
Usage
j_data_type(data)
j_path_type(path)
Arguments
data |
a character() JSON string or NDJSON records, or the
name of a file or URL containing JSON or NDJSON, or an R
object parsed to a JSON string using |
path |
character(1) JSONpointer, JSONpath or JMESpath query string. |
Details
j_data_type()
without any arguments reports possible return
values: "json"
, "ndjson"
, "file"
, "url"
, "R"
. When
provided an argument, j_data_type()
infers (but does not
validate) the type of data
based on the following rules:
For a scalar (length 1) character
data
, either"url"
(matching regular expression"^https?://"
,"file"
(file.exists(data)
returnsTRUE
), or"json"
. When"file"
or"url"
is inferred, the return value is a length 2 vector, with the first element the inferred type of data ("json"
or"ndjson"
) obtained from the first 2 lines of the file.For character data with
length(data) > 1
,"ndjson"
if all elements start a square bracket or curly brace, consistently (i.e., agreeing with the start of the first record), otherwise"json"
.-
"R"
for all non-character data.
j_path_type()
without any argument reports possible values:
"JSONpointer"
, "JSONpath"
, or "JMESpath"
. When provided an
argument, j_path_type()
infers the type of path
using a simple
but incomplete classification:
-
"JSONpointer"
is inferred if the the path is""
or starts with"/"
. -
"JSONpath"
expressions start with"$"
. -
"JMESpath"
expressions satisfy neither theJSONpointer
norJSONpath
criteria.
Because of these rules, the valid JSONpointer path "@"
is
interpreted as JMESpath; use jsonpointer()
if JSONpointer
behavior is required.
Examples
j_data_type() # available types
j_data_type("") # json
j_data_type('{"a": 1}') # json
j_data_type(c('[{"a": 1}', '{"a": 2}]')) # json
j_data_type(c('{"a": 1}', '{"a": 2}')) # ndjson
j_data_type(list(a = 1, b = 2)) # R
fl <- system.file(package = "rjsoncons", "extdata", "example.json")
j_data_type(fl) # c('json', 'file')
j_data_type(readLines(fl)) # json
j_path_type() # available types
j_path_type("") # JSONpointer
j_path_type("/locations/0/name") # JSONpointer
j_path_type("$.locations[0].name") # JSONpath
j_path_type("locations[0].name") # JMESpath