Skip to contents

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 jsonlite::toJSON().

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) returns TRUE), 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 the JSONpointer nor JSONpath 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
#> [[1]]
#> [1] "json"
#> 
#> [[2]]
#> [1] "ndjson"
#> 
#> [[3]]
#> [1] "json" "file"
#> 
#> [[4]]
#> [1] "ndjson" "file"  
#> 
#> [[5]]
#> [1] "json" "url" 
#> 
#> [[6]]
#> [1] "ndjson" "url"   
#> 
#> [[7]]
#> [1] "R"
#> 
j_data_type("")                          # json
#> [1] "R"
j_data_type('{"a": 1}')                  # json
#> [1] "json"
j_data_type(c('[{"a": 1}', '{"a": 2}]')) # json
#> [1] "json"
j_data_type(c('{"a": 1}', '{"a": 2}'))   # ndjson
#> [1] "ndjson"
j_data_type(list(a = 1, b = 2))          # R
#> [1] "R"
fl <- system.file(package = "rjsoncons", "extdata", "example.json")
j_data_type(fl)                          # c('json', 'file')
#> [1] "json" "file"
j_data_type(readLines(fl))               # json
#> [1] "json"

j_path_type()                            # available types
#> [1] "JSONpointer" "JSONpath"    "JMESpath"   
j_path_type("")                          # JSONpointer
#> [1] "JSONpointer"
j_path_type("/locations/0/name")         # JSONpointer
#> [1] "JSONpointer"
j_path_type("$.locations[0].name")       # JSONpath
#> [1] "JSONpath"
j_path_type("locations[0].name")         # JMESpath
#> [1] "JMESpath"