Skip to content

Commit 740a91d

Browse files
committed
add example using j_query() for rbindlist() / bind_rows()
1 parent 0615f8e commit 740a91d

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

.github/workflows/pkgdown.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- uses: r-lib/actions/[email protected]
3838
with:
3939
cache-version: 2
40-
extra-packages: any::pkgdown, any::dplyr, any::tidyr, any::jqr, any::rvest, any::tidygraph, any::ggraph, local::.
40+
extra-packages: any::pkgdown, any::dplyr, any::tidyr, any::jqr, any::rvest, any::tidygraph, any::ggraph, any::data.table, any::rvest, local::.
4141
needs: website
4242

4343
- name: Build site

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: rjsoncons
22
Title: Query, Pivot, Patch, and Validate 'JSON' and 'NDJSON'
3-
Version: 1.3.1.9000
3+
Version: 1.3.1.9001
44
Authors@R: c(
55
person(
66
"Martin", "Morgan", role = c("aut", "cre"),

NEWS.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# rjsoncons 1.3.2
1+
# Pre-release
22

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

vignettes/articles/c_examples.Rmd

+79
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,85 @@ The result is
193193
## # ℹ Use `print(n = ...)` to see more rows
194194
```
195195

196+
## Reshaping nested records
197+
198+
<https://stackoverflow.com/questions/78952424>
199+
200+
This question wants to transform JSON into a [data.table][]. A
201+
previous answer uses `rbindlist()` (similar to `dplyr::bind_rows()`)
202+
to transform structured lists to data.tables. Here is the sample data
203+
204+
```{r}
205+
json <- '[
206+
{
207+
"version_id": "123456",
208+
"data": [
209+
{
210+
"review_id": "1",
211+
"rating": 5,
212+
"review": "This app is great",
213+
"date": "2024-09-01"
214+
},
215+
{
216+
"review_id": "2",
217+
"rating": 1,
218+
"review": "This app is terrible",
219+
"date": "2024-09-01"
220+
}
221+
]
222+
},
223+
{
224+
"version_id": "789101",
225+
"data": [
226+
{
227+
"review_id": "3",
228+
"rating": 3,
229+
"review": "This app is OK",
230+
"date": "2024-09-01"
231+
}
232+
]
233+
}
234+
]'
235+
```
236+
237+
The desired data.table is flattened to include `version_id` and each
238+
field of `data[]` as columns in the table, with the complication that
239+
`version_id` needs to be replicated for each element of `data[]`.
240+
241+
The rjsoncons [answer][78952424/547331] illustrates several
242+
approaches. The approach most cleanly separating data transformation
243+
and data.table construction using [JMESPath][] create an array of
244+
objects where each `version_id` is associated with *vectors* of
245+
`review_id`, etc., corresponding to that version.
246+
247+
```{r}
248+
query <-
249+
"[].{
250+
version_id: version_id,
251+
review_id: data[].review_id,
252+
rating: data[].rating,
253+
review: data[].review,
254+
date: data[].date
255+
}"
256+
```
257+
258+
As an *R* object, this is exactly handled by `rbindlist()`. Note that
259+
a pivot is not involved.
260+
261+
```{r}
262+
records <- j_query(json, query, as = "R")
263+
data.table::rbindlist(records)
264+
```
265+
266+
[dplyr][]'s `bind_rows()` behaves similarly:
267+
268+
```{r}
269+
dplyr::bind_rows(records)
270+
```
271+
272+
[data.table]: https://CRAN.R-project.org/package=data.table
273+
[78952424/547331]: https://stackoverflow.com/a/78967138/547331
274+
196275
## Reading from URLs
197276

198277
<https://stackoverflow.com/questions/78023560>

0 commit comments

Comments
 (0)