json_structure: Recursively structures arbitrary JSON data into a single data...

View source: R/json_structure.R

json_structureR Documentation

Recursively structures arbitrary JSON data into a single data frame

Description

Returns a tbl_json object where each row corresponds to a leaf in the JSON structure. The first row corresponds to the JSON document as a whole. If the document is a scalar value (JSON string, number, logical or null), then there will only be 1 row. If instead it is an object or an array, then subsequent rows will recursively correspond to the elements (and their children) of the object or array.

Usage

json_structure(.x)

Arguments

.x

a json string or tbl_json object

Details

The columns in the tbl_json returend are defined as

  • document.id 1L if .x is a single JSON string, otherwise the index of .x.

  • parent.id the string identifier of the parent node for this child.

  • level what level of the hierarchy this child resides at, starting at 0L for the root and incrementing for each level of nested array or object.

  • index what index of the parent object / array this child resides at (from gather_array for arrays).

  • child.id a unique ID for this leaf in this document, represented as <parent>.<index> where <parent> is the ID for the parent and <index> is this index.

  • seq the sequence of names / indices that led to this child (parents that are arrays are excluded) as a list, where character strings denote objects and integers denote array positions

  • name if this is the value of an object, what was the name that it is listed under (from gather_object).

  • type the type of this object (from json_types).

  • length the length of this object (from json_lengths).

Value

a tbl_json object

See Also

json_schema to create a schema for a JSON document or collection

Examples


# A simple string
'"string"' %>% json_structure

# A simple object
'{"name": "value"}' %>% json_structure

# A complex array
'[{"a": 1}, [1, 2], "a", 1, true, null]' %>% json_structure

# A sample of structure rows from a company
library(dplyr)
companies[1] %>% json_structure %>% sample_n(5)

jeremystan/tidyjson documentation built on Feb. 4, 2023, 6:54 p.m.