-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten.R
359 lines (337 loc) · 10.8 KB
/
flatten.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
## internal implementation of .j_flatten, always returns a list to
## simplify j_find_*() processing of both JSON & NDJSON
.j_flatten <-
function(
data, object_names, as, ..., n_records, verbose,
data_type, path_type)
{
## initialize constants to enable code re-use
path <- switch(
path_type, JSONpointer = "", JSONpath = "$",
stop("unsupported path_type '", path_type, "'", call. = FALSE)
)
## validity
.j_valid(data_type, object_names, path, path_type, n_records, verbose)
data <- .as_json_string(data, data_type, ...)
result <- do_cpp(
cpp_j_flatten, cpp_j_flatten_con,
data, data_type, object_names, as, path, path_type,
n_records = n_records, verbose = verbose
)
}
## internal function calling grepl with argument list
.j_find_grepl <-
function(pattern, x, grep_args)
{
stopifnot(
is.list(grep_args),
all(
names(grep_args) %in%
setdiff(names(formals(grepl)), c("pattern", "x"))
)
)
args <- c(list(pattern = pattern, x = x), grep_args)
do.call(grepl, args)
}
## internal function to find keys from JSONpointer or JSONpath paths
.j_find_keys_from_path <-
function(path, path_type)
{
if (identical(path_type, "JSONpointer")) {
path <- sub("^/", "", path)
strsplit(path, "/")
} else if (identical(path_type, "JSONpath")) {
## FIXME: the path needs to be parsed propertly, not via
## regex. Also, paths contain enough information to
## distinguish between string and integer keys, but all keys
## are treated as integers
path <- sub("^\\$", "", path)
path <- sub("^\\[(.*)\\]$", "\\1", path)
keys0 <- strsplit(path, "][", fixed = TRUE)
lapply(keys0, sub, pattern = "^'(.*)'$", replacement = "\\1")
} else {
stop("unsupported path_type '", path_type, "'", call. = FALSE)
}
}
## internal function to format j_find_*() result
.j_find_format <-
function(flattened, as, data_type)
{
result <- lapply(flattened, function(json_record, as) {
if (identical(as, "R")) {
json_record
} else {
paths <- names(json_record)
values <- unlist(json_record, use.names = FALSE)
switch(
as,
data.frame = data.frame(path = paths, value = values),
tibble = tibble::tibble(path = paths, value = values)
)
}
}, as)
if (data_type[[1]] %in% c("json", "R")) # not NDJSON
result <- result[[1]]
result
}
#' @rdname flatten
#'
#' @title Flatten and find keys or values in JSON or NDJSON documents
#'
#' @description `j_flatten()` transforms a JSON document into a list
#' where names are JSONpointer 'paths' and elements are the
#' corresponding 'values' from the JSON document.
#'
#' @inheritParams j_query
#'
#' @param as character(1) describing the return type. For
#' `j_flatten()`, either "string" or "R". For other functions on
#' this page, one of "R", "data.frame", or "tibble".
#'
#' @param path_type character(1) type of 'path' to be returned; one of
#' '"JSONpointer"', '"JSONpath"'; '"JMESpath"' is not supported.
#'
#' @details Functions documented on this page expand `data` into all
#' path / value pairs. This is not suitable for very large JSON
#' documents.
#'
#' @return
#'
#' `j_flatten(as = "string")` (default) returns a JSON string
#' representation of the flattened document, i.e., an object with keys
#' the JSONpointer paths and values the value at the corresponding
#' path in the original document.
#'
#' `j_flatten(as = "R")` returns a named list, where `names()` are the
#' JSONpointer paths to each element in the JSON document and list
#' elements are the corresponding values.
#'
#' @examples
#' json <- '{
#' "discards": {
#' "1000": "Record does not exist",
#' "1004": "Queue limit exceeded",
#' "1010": "Discarding timed-out partial msg"
#' },
#' "warnings": {
#' "0": "Phone number missing country code",
#' "1": "State code missing",
#' "2": "Zip code missing"
#' }
#' }'
#'
#' ## JSONpointer
#' j_flatten(json) |>
#' cat("\n")
#'
#' ## JSONpath
#' j_flatten(json, as = "R", path_type = "JSONpath") |>
#' str()
#'
#' @export
j_flatten <-
function(
data, object_names = "asis", as = "string", ...,
n_records = Inf, verbose = FALSE,
data_type = j_data_type(data), path_type = "JSONpointer"
)
{
stopifnot(.is_scalar_character(as), as %in% c("string", "R"))
result <- .j_flatten(
data, object_names, as, ..., n_records = n_records, verbose = verbose,
data_type = data_type, path_type = path_type
)
if (data_type[[1]] %in% c("json", "R"))
result <- result[[1]]
result
}
#' @rdname flatten
#'
#' @description `j_find_values()` finds paths to exactly matching
#' values.
#'
#' @param values vector of one or more values to be matched exactly to
#' values in the JSON document.
#'
#' @return `j_find_values()` and `j_find_values_grep()` return a list
#' with names as JSONpointer paths and list elements the matching
#' values, or a `data.frame` or `tibble` with columns `path` and
#' `value`. Values are coerced to a common type when `as` is
#' `data.frame` or `tibble`.
#'
#' @examples
#' j_find_values(json, "Zip code missing", as = "tibble")
#' j_find_values(
#' json,
#' c("Queue limit exceeded", "Zip code missing"),
#' as = "tibble"
#' )
#'
#' @export
j_find_values <-
function(
data, values, object_names = "asis", as = "R", ...,
n_records = Inf, verbose = FALSE,
data_type = j_data_type(data), path_type = "JSONpointer"
)
{
stopifnot(
.is_scalar_character(as), as %in% c("R", "data.frame", "tibble")
)
result <- .j_flatten(
data, object_names, "R", ..., n_records = n_records, verbose = verbose,
data_type = data_type, path_type = path_type
)
flattened <- lapply(result, function(json_record) {
Filter(\(x) x %in% values, json_record)
})
.j_find_format(flattened, as, data_type)
}
#' @rdname flatten
#'
#' @description `j_find_values_grep()` finds paths to values matching
#' a regular expression.
#'
#' @param pattern character(1) regular expression to match values or
#' paths.
#'
#' @param grep_args list() additional arguments passed to `grepl()`
#' when searching on values or paths.
#'
#' @examples
#' j_find_values_grep(json, "missing", as = "tibble")
#'
#' ## JSONpath
#' j_find_values_grep(json, "missing", as = "tibble", path_type = "JSONpath")
#'
#' @export
j_find_values_grep <-
function(
data, pattern, object_names = "asis", as = "R", ...,
grep_args = list(), n_records = Inf, verbose = FALSE,
data_type = j_data_type(data), path_type = "JSONpointer"
)
{
stopifnot(
.is_scalar_character(pattern),
.is_scalar_character(as), as %in% c("R", "data.frame", "tibble")
## FIXME: validate grep_args
)
result <- .j_flatten(
data, object_names, "R", ..., n_records = n_records, verbose = verbose,
data_type = data_type, path_type = path_type
)
flattened <- lapply(result, function(json_record, grep_args) {
values <- unlist(json_record, use.names = FALSE)
idx <- .j_find_grepl(pattern, values, grep_args)
json_record[idx]
}, grep_args)
.j_find_format(flattened, as, data_type)
}
#' @rdname flatten
#'
#' @description `j_find_keys()` finds paths to exactly matching keys.
#'
#' @param keys character() vector of one or more keys to be matched
#' exactly to path elements.
#'
#' @details For `j_find_keys()`, the `key` must exactly match one or
#' more consecutive keys in the JSONpointer path returned by
#' `j_flatten()`.
#'
#' @return `j_find_keys()` and `j_find_keys_grep()` returns a list,
#' data.frame, or tibble similar to `j_find_values()` and
#' `j_find_values_grep()`.
#'
#' @examples
#' j_find_keys(json, "discards", as = "tibble")
#' j_find_keys(json, "1", as = "tibble")
#' j_find_keys(json, c("discards", "warnings"), as = "tibble")
#'
#' ## JSONpath
#' j_find_keys(json, "discards", as = "tibble", path_type = "JSONpath")
#'
#' @export
j_find_keys <-
function(
data, keys, object_names = "asis", as = "R", ...,
n_records = Inf, verbose = FALSE,
data_type = j_data_type(data), path_type = "JSONpointer"
)
{
stopifnot(
is.character(keys), !anyNA(keys),
.is_scalar_character(as), as %in% c("R", "data.frame", "tibble")
)
result <- .j_flatten(
data, object_names, "R", ..., n_records = n_records, verbose = verbose,
data_type = data_type, path_type = path_type
)
flattened <- lapply(result, function(json_record) {
keys0 <- .j_find_keys_from_path(names(json_record), path_type)
idx0 <- unlist(keys0) %in% keys
idx <- unique(rep(seq_along(keys0), lengths(keys0))[idx0])
json_record[idx]
})
.j_find_format(flattened, as, data_type)
}
#' @rdname flatten
#'
#' @description `j_find_keys_grep()` finds paths to keys matching a
#' regular expression.
#'
#' @details For `j_find_keys_grep()`, the `key` can define a pattern
#' that spans across JSONpointer or JSONpath elements.
#'
#' @examples
#' j_find_keys_grep(json, "discard", as = "tibble")
#' j_find_keys_grep(json, "1", as = "tibble")
#' j_find_keys_grep(json, "car.*/101", as = "tibble")
#'
#' ## JSONpath
#' j_find_keys_grep(json, "car.*\\['101", as = "tibble", path_type = "JSONpath")
#'
#' @export
j_find_keys_grep <-
function(
data, pattern, object_names = "asis", as = "R", ...,
grep_args = list(), n_records = Inf, verbose = FALSE,
data_type = j_data_type(data), path_type = "JSONpointer"
)
{
stopifnot(
.is_scalar_character(pattern),
.is_scalar_character(as), as %in% c("R", "data.frame", "tibble")
)
result <- .j_flatten(
data, object_names, "R", ..., n_records = n_records, verbose = verbose,
data_type = data_type, path_type = path_type
)
flattened <- lapply(result, function(json_record, grep_args) {
idx <- .j_find_grepl(pattern, names(json_record), grep_args)
json_record[idx]
}, grep_args)
.j_find_format(flattened, as, data_type)
}
#' @rdname flatten
#'
#' @name flatten_NDJSON
#'
#' @description For NDJSON documents, the result is either a character
#' vector (for `as = "string"`) or list of *R* objects, one
#' element for each NDJSON record.
#'
#' @return For NDJSON documents, the result is a vector paralleling
#' the NDJSON document, with `j_flatten()` applied to each element
#' of the NDJSON document.
#'
#' @examples
#' ## NDJSON
#'
#' ndjson_file <-
#' system.file(package = "rjsoncons", "extdata", "example.ndjson")
#' j_flatten(ndjson_file) |>
#' noquote()
#' j_find_values_grep(ndjson_file, "e") |>
#' str()
NULL