Creates an intermediate definition for a JSON array where all elements must conform
to the same specified element_structure
. This definition is used within
s_map()
or finalized by build_structure()
.
Arguments
- element_structure
The structure definition for the elements within the vector. This should be the result of another
s_*
function call (e.g.,s_integer()
,s_map(id = s_integer())
).
Examples
# Define a vector of strings
vec_str_def <- s_vector(s_string())
build_structure(vec_str_def)
#> Vector(
#> String,
#> )
# Expected: list(type = "vector", value = list(type = "string"))
# Define a vector of objects, each having an 'id' (integer) and 'name' (string)
vec_obj_def <- s_vector(
s_map(id = s_integer(), name = s_string())
)
build_structure(vec_obj_def)
#> Vector(
#> Map {
#> fields: {
#> "name": String,
#> "id": Integer,
#> },
#> ignore_extra_fields: false,
#> expected_fields_str: [
#> "name",
#> "id",
#> ],
#> },
#> )
# Expected: list(type = "vector", value = list(type = "map",
# value = list(id = list(type = "integer"),
# name = list(type = "string"))))
# Parsing example (see ?parse_json)
json_data <- '[{"id": 1, "name": "A"}, {"id": 2, "name": "B"}]'
parsed <- parse_json(json_data, build_structure(vec_obj_def))
print(parsed) # Will be a list of lists
#> [[1]]
#> [[1]]$id
#> [1] 1
#>
#> [[1]]$name
#> [1] "A"
#>
#>
#> [[2]]
#> [[2]]$id
#> [1] 2
#>
#> [[2]]$name
#> [1] "B"
#>
#>