If reading as data.frame, each row of NDJSON becomes a row in the data.frame. If reading as a list, then each row becomes an element in the list.

read_ndjson_raw(
  x,
  type = c("df", "list"),
  nread = -1,
  nskip = 0,
  nprobe = 100,
  opts = list(),
  ...
)

Arguments

x

string containing NDJSON

type

The type of R object the JSON should be parsed into. Valid values are 'df' or 'list'. Default: 'df' (data.frame)

nread

Number of records to read. Default: -1 (reads all JSON strings)

nskip

Number of records to skip before starting to read. Default: 0 (skip no data)

nprobe

Number of lines to read to determine types for data.frame columns. Default: 100. Use -1 to probe entire file.

opts

Named list of options for parsing. Usually created by opts_read_json()

...

Other named options can be used to override any options in opts. The valid named options are identical to arguments to opts_read_json()

Value

NDJSON data read into R as list or data.frame depending on 'type' argument

Details

If parsing NDJSON to a data.frame it is usually better if the json objects are consistent from line-to-line. Type inference for the data.frame is done during initialisation by reading through nprobe lines. Warning: if there is a type-mismatch further into the file than it is probed, then you will get missing values in the data.frame, or JSON values not captured in the R data.

No flattening of the namespace is done i.e. nested object remain nested.

Examples

js <- write_ndjson_raw(head(mtcars))
js
#>   [1] 7b 22 6d 70 67 22 3a 32 31 2e 30 2c 22 63 79 6c 22 3a 36 2e 30 2c 22 64 69
#>  [26] 73 70 22 3a 31 36 30 2e 30 2c 22 68 70 22 3a 31 31 30 2e 30 2c 22 64 72 61
#>  [51] 74 22 3a 33 2e 39 2c 22 77 74 22 3a 32 2e 36 32 2c 22 71 73 65 63 22 3a 31
#>  [76] 36 2e 34 36 2c 22 76 73 22 3a 30 2e 30 2c 22 61 6d 22 3a 31 2e 30 2c 22 67
#> [101] 65 61 72 22 3a 34 2e 30 2c 22 63 61 72 62 22 3a 34 2e 30 7d 0a 7b 22 6d 70
#> [126] 67 22 3a 32 31 2e 30 2c 22 63 79 6c 22 3a 36 2e 30 2c 22 64 69 73 70 22 3a
#> [151] 31 36 30 2e 30 2c 22 68 70 22 3a 31 31 30 2e 30 2c 22 64 72 61 74 22 3a 33
#> [176] 2e 39 2c 22 77 74 22 3a 32 2e 38 37 35 2c 22 71 73 65 63 22 3a 31 37 2e 30
#> [201] 32 2c 22 76 73 22 3a 30 2e 30 2c 22 61 6d 22 3a 31 2e 30 2c 22 67 65 61 72
#> [226] 22 3a 34 2e 30 2c 22 63 61 72 62 22 3a 34 2e 30 7d 0a 7b 22 6d 70 67 22 3a
#> [251] 32 32 2e 38 2c 22 63 79 6c 22 3a 34 2e 30 2c 22 64 69 73 70 22 3a 31 30 38
#> [276] 2e 30 2c 22 68 70 22 3a 39 33 2e 30 2c 22 64 72 61 74 22 3a 33 2e 38 35 2c
#> [301] 22 77 74 22 3a 32 2e 33 32 2c 22 71 73 65 63 22 3a 31 38 2e 36 31 2c 22 76
#> [326] 73 22 3a 31 2e 30 2c 22 61 6d 22 3a 31 2e 30 2c 22 67 65 61 72 22 3a 34 2e
#> [351] 30 2c 22 63 61 72 62 22 3a 31 2e 30 7d 0a 7b 22 6d 70 67 22 3a 32 31 2e 34
#> [376] 2c 22 63 79 6c 22 3a 36 2e 30 2c 22 64 69 73 70 22 3a 32 35 38 2e 30 2c 22
#> [401] 68 70 22 3a 31 31 30 2e 30 2c 22 64 72 61 74 22 3a 33 2e 30 38 2c 22 77 74
#> [426] 22 3a 33 2e 32 31 35 2c 22 71 73 65 63 22 3a 31 39 2e 34 34 2c 22 76 73 22
#> [451] 3a 31 2e 30 2c 22 61 6d 22 3a 30 2e 30 2c 22 67 65 61 72 22 3a 33 2e 30 2c
#> [476] 22 63 61 72 62 22 3a 31 2e 30 7d 0a 7b 22 6d 70 67 22 3a 31 38 2e 37 2c 22
#> [501] 63 79 6c 22 3a 38 2e 30 2c 22 64 69 73 70 22 3a 33 36 30 2e 30 2c 22 68 70
#> [526] 22 3a 31 37 35 2e 30 2c 22 64 72 61 74 22 3a 33 2e 31 35 2c 22 77 74 22 3a
#> [551] 33 2e 34 34 2c 22 71 73 65 63 22 3a 31 37 2e 30 32 2c 22 76 73 22 3a 30 2e
#> [576] 30 2c 22 61 6d 22 3a 30 2e 30 2c 22 67 65 61 72 22 3a 33 2e 30 2c 22 63 61
#> [601] 72 62 22 3a 32 2e 30 7d 0a 7b 22 6d 70 67 22 3a 31 38 2e 31 2c 22 63 79 6c
#> [626] 22 3a 36 2e 30 2c 22 64 69 73 70 22 3a 32 32 35 2e 30 2c 22 68 70 22 3a 31
#> [651] 30 35 2e 30 2c 22 64 72 61 74 22 3a 32 2e 37 36 2c 22 77 74 22 3a 33 2e 34
#> [676] 36 2c 22 71 73 65 63 22 3a 32 30 2e 32 32 2c 22 76 73 22 3a 31 2e 30 2c 22
#> [701] 61 6d 22 3a 30 2e 30 2c 22 67 65 61 72 22 3a 33 2e 30 2c 22 63 61 72 62 22
#> [726] 3a 31 2e 30 7d 00
read_ndjson_raw(js, 'list')
#> [[1]]
#> [[1]]$mpg
#> [1] 21
#> 
#> [[1]]$cyl
#> [1] 6
#> 
#> [[1]]$disp
#> [1] 160
#> 
#> [[1]]$hp
#> [1] 110
#> 
#> [[1]]$drat
#> [1] 3.9
#> 
#> [[1]]$wt
#> [1] 2.62
#> 
#> [[1]]$qsec
#> [1] 16.46
#> 
#> [[1]]$vs
#> [1] 0
#> 
#> [[1]]$am
#> [1] 1
#> 
#> [[1]]$gear
#> [1] 4
#> 
#> [[1]]$carb
#> [1] 4
#> 
#> 
#> [[2]]
#> [[2]]$mpg
#> [1] 21
#> 
#> [[2]]$cyl
#> [1] 6
#> 
#> [[2]]$disp
#> [1] 160
#> 
#> [[2]]$hp
#> [1] 110
#> 
#> [[2]]$drat
#> [1] 3.9
#> 
#> [[2]]$wt
#> [1] 2.875
#> 
#> [[2]]$qsec
#> [1] 17.02
#> 
#> [[2]]$vs
#> [1] 0
#> 
#> [[2]]$am
#> [1] 1
#> 
#> [[2]]$gear
#> [1] 4
#> 
#> [[2]]$carb
#> [1] 4
#> 
#> 
#> [[3]]
#> [[3]]$mpg
#> [1] 22.8
#> 
#> [[3]]$cyl
#> [1] 4
#> 
#> [[3]]$disp
#> [1] 108
#> 
#> [[3]]$hp
#> [1] 93
#> 
#> [[3]]$drat
#> [1] 3.85
#> 
#> [[3]]$wt
#> [1] 2.32
#> 
#> [[3]]$qsec
#> [1] 18.61
#> 
#> [[3]]$vs
#> [1] 1
#> 
#> [[3]]$am
#> [1] 1
#> 
#> [[3]]$gear
#> [1] 4
#> 
#> [[3]]$carb
#> [1] 1
#> 
#> 
#> [[4]]
#> [[4]]$mpg
#> [1] 21.4
#> 
#> [[4]]$cyl
#> [1] 6
#> 
#> [[4]]$disp
#> [1] 258
#> 
#> [[4]]$hp
#> [1] 110
#> 
#> [[4]]$drat
#> [1] 3.08
#> 
#> [[4]]$wt
#> [1] 3.215
#> 
#> [[4]]$qsec
#> [1] 19.44
#> 
#> [[4]]$vs
#> [1] 1
#> 
#> [[4]]$am
#> [1] 0
#> 
#> [[4]]$gear
#> [1] 3
#> 
#> [[4]]$carb
#> [1] 1
#> 
#> 
#> [[5]]
#> [[5]]$mpg
#> [1] 18.7
#> 
#> [[5]]$cyl
#> [1] 8
#> 
#> [[5]]$disp
#> [1] 360
#> 
#> [[5]]$hp
#> [1] 175
#> 
#> [[5]]$drat
#> [1] 3.15
#> 
#> [[5]]$wt
#> [1] 3.44
#> 
#> [[5]]$qsec
#> [1] 17.02
#> 
#> [[5]]$vs
#> [1] 0
#> 
#> [[5]]$am
#> [1] 0
#> 
#> [[5]]$gear
#> [1] 3
#> 
#> [[5]]$carb
#> [1] 2
#> 
#> 
#> [[6]]
#> [[6]]$mpg
#> [1] 18.1
#> 
#> [[6]]$cyl
#> [1] 6
#> 
#> [[6]]$disp
#> [1] 225
#> 
#> [[6]]$hp
#> [1] 105
#> 
#> [[6]]$drat
#> [1] 2.76
#> 
#> [[6]]$wt
#> [1] 3.46
#> 
#> [[6]]$qsec
#> [1] 20.22
#> 
#> [[6]]$vs
#> [1] 1
#> 
#> [[6]]$am
#> [1] 0
#> 
#> [[6]]$gear
#> [1] 3
#> 
#> [[6]]$carb
#> [1] 1
#> 
#> 
read_ndjson_raw(js, 'df')
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1