rjson: JSON for R

JSONファイルをRでやりとりする

> library(rjson)

Attaching package: 'rjson'
The following objects are masked from 'package:RJSONIO':

    fromJSON, toJSON
The following objects are masked from 'package:jsonlite':

    fromJSON, toJSON

バージョン: 0.2.15


関数名 概略
fromJSON Convert JSON To R
newJSONParser Convert buffered JSON objects To R
rjson JSON for R
toJSON Convert R To JSON

fromJSON

JSON形式からRオブジェクトへの変換

ref) jsonlite::fromJSON(), RJSONIO::fromJSON()

Arguments

  • json_str
  • file
  • method
  • unexpected.escape
> test_json <- c('
+ [
+  {"name": "Yamada", "number": 23},
+  {"name": "Matsui", "number": 55},
+  {"name": "Suzuki", "number": 51}
+ ]')
> rjson::fromJSON(json_str = test_json)
[[1]]
[[1]]$name
[1] "Yamada"

[[1]]$number
[1] 23


[[2]]
[[2]]$name
[1] "Matsui"

[[2]]$number
[1] 55


[[3]]
[[3]]$name
[1] "Suzuki"

[[3]]$number
[1] 51

toJSON

RオブジェクトからJSON形式に変換

ref) jsonlite::toJSON(), RJSONIO::toJSON()

> rjson::toJSON(x = test_json)
[1] "\"\\n[\\n {\\\"name\\\": \\\"Yamada\\\", \\\"number\\\": 23},\\n {\\\"name\\\": \\\"Matsui\\\", \\\"number\\\": 55},\\n {\\\"name\\\": \\\"Suzuki\\\", \\\"number\\\": 51}\\n]\""
> x <- list(alpha = 1:5, beta = "Bravo", 
+           gamma = list(a = 1:3, b = NULL), 
+           delta = c(TRUE, FALSE) )
> (json <- toJSON(x))
[1] "{\"alpha\":[1,2,3,4,5],\"beta\":\"Bravo\",\"gamma\":{\"a\":[1,2,3],\"b\":null},\"delta\":[true,false]}"
> fromJSON(json)
$alpha
[1] 1 2 3 4 5

$beta
[1] "Bravo"

$gamma
$gamma$a
[1] 1 2 3

$gamma$b
NULL


$delta
[1]  TRUE FALSE