@@ -193,6 +193,85 @@ The result is
193
193
## # ℹ Use `print(n = ...)` to see more rows
194
194
```
195
195
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
+
196
275
## Reading from URLs
197
276
198
277
< https://stackoverflow.com/questions/78023560 >
0 commit comments