Skip to content

Commit

Permalink
add example using j_query() for rbindlist() / bind_rows()
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmorgan committed Sep 10, 2024
1 parent 0615f8e commit 740a91d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2.7.2
with:
cache-version: 2
extra-packages: any::pkgdown, any::dplyr, any::tidyr, any::jqr, any::rvest, any::tidygraph, any::ggraph, local::.
extra-packages: any::pkgdown, any::dplyr, any::tidyr, any::jqr, any::rvest, any::tidygraph, any::ggraph, any::data.table, any::rvest, local::.
needs: website

- name: Build site
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rjsoncons
Title: Query, Pivot, Patch, and Validate 'JSON' and 'NDJSON'
Version: 1.3.1.9000
Version: 1.3.1.9001
Authors@R: c(
person(
"Martin", "Morgan", role = c("aut", "cre"),
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# rjsoncons 1.3.2
# Pre-release

- (1.3.1.9001) add example illustrating `j_query()` with JSON
reformatting to directly suit `datatable::rbindlist()` or
`dplyr::bind_rows()`.
- (1.3.1.9000) add second example illustrating construction of
pivotable objects.

Expand Down
79 changes: 79 additions & 0 deletions vignettes/articles/c_examples.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,85 @@ The result is
## # ℹ Use `print(n = ...)` to see more rows
```

## Reshaping nested records

<https://stackoverflow.com/questions/78952424>

This question wants to transform JSON into a [data.table][]. A
previous answer uses `rbindlist()` (similar to `dplyr::bind_rows()`)
to transform structured lists to data.tables. Here is the sample data

```{r}
json <- '[
{
"version_id": "123456",
"data": [
{
"review_id": "1",
"rating": 5,
"review": "This app is great",
"date": "2024-09-01"
},
{
"review_id": "2",
"rating": 1,
"review": "This app is terrible",
"date": "2024-09-01"
}
]
},
{
"version_id": "789101",
"data": [
{
"review_id": "3",
"rating": 3,
"review": "This app is OK",
"date": "2024-09-01"
}
]
}
]'
```

The desired data.table is flattened to include `version_id` and each
field of `data[]` as columns in the table, with the complication that
`version_id` needs to be replicated for each element of `data[]`.

The rjsoncons [answer][78952424/547331] illustrates several
approaches. The approach most cleanly separating data transformation
and data.table construction using [JMESPath][] create an array of
objects where each `version_id` is associated with *vectors* of
`review_id`, etc., corresponding to that version.

```{r}
query <-
"[].{
version_id: version_id,
review_id: data[].review_id,
rating: data[].rating,
review: data[].review,
date: data[].date
}"
```

As an *R* object, this is exactly handled by `rbindlist()`. Note that
a pivot is not involved.

```{r}
records <- j_query(json, query, as = "R")
data.table::rbindlist(records)
```

[dplyr][]'s `bind_rows()` behaves similarly:

```{r}
dplyr::bind_rows(records)
```

[data.table]: https://CRAN.R-project.org/package=data.table
[78952424/547331]: https://stackoverflow.com/a/78967138/547331

## Reading from URLs

<https://stackoverflow.com/questions/78023560>
Expand Down

0 comments on commit 740a91d

Please sign in to comment.