将JavaScript对象数组转换为R编程语言中的JSON。

huangapple go评论74阅读模式
英文:

Convert JavaScript array of objects to JSON in r programming language

问题

我需要将 JSON 对象传递给 R 编程中的一个函数。以下是可以传递的各种 JavaScript 对象示例,需要在 R 中将它们转换为 JSON。

Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]

Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]

我需要使用 jsonlite 包将它们转换为 R 中的 JSON 对象。

英文:

I need to pass JSON object to a function in r programming. The following are the various examples of JavaScript objects that can be passed which needed to be converted to JSON in r

Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]

Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]

I need help converting them into a JSON object in r using jsonlite package.

答案1

得分: 0

JavaScript代码可以使用`v8`进行评估,该代码可以包括`JSON.stringify()`,但对于一系列简单的赋值表达式,我们可以为`$eval()`方法设置`serialize`参数,它将返回JSON字符串:

library(V8)
#> 使用V8引擎9.1.269.38
js <- c(
  ex1 = ''Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]'',
  ex2 = ''Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]''
)

ctx <- v8()
# 评估并序列化js返回对象
json_1 <- ctx$eval(js[1], serialize = TRUE) 
jsonlite::validate(json_1)
#> [1] TRUE
json_1
#> [1] "[{\"name\":\"Orange\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"fruit\"},{\"name\":\"Apple\",\"data\":{\"Tuesday\":1,\"Friday\":8},\"stack\":\"fruit\"},{\"name\":\"Cucumber\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"vegetable\"},{\"name\":\"Carrot\",\"data\":{\"Tuesday\":1,\"Friday\":8},\"stack\":\"vegetable\"}]"

# 或者在表达式列表上应用ctx$eval()
json_lst <- lapply(js, ctx$eval, serialize = TRUE)
str(json_lst)
#> 2个元素的列表
#>  $ ex1: chr "[{\"name\":\"Orange\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"fruit\"},{\"name\":\"Apple\",\"data\":{"| __truncated__
#>  $ ex2: chr "[[\"Washington\",\"1789-04-29\",\"1797-03-03\"],[\"Adams\",\"1797-03-03\",\"1801-03-03\"]]"
英文:

Javascript code can be evaluated with v8, that code can include JSON.stringify(), but for a list of simple assignment expressions we can set serialize parameter for $eval() method and it will return JSON string:

library(V8)
#> Using V8 engine 9.1.269.38
js <- c(
  ex1 = 'Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]',
  ex2 = 'Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]'
)

ctx <- v8()
# evaluate and serialize js return object
json_1 <- ctx$eval(js[1], serialize = TRUE) 
jsonlite::validate(json_1)
#> [1] TRUE
json_1
#> [1] "[{\"name\":\"Orange\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"fruit\"},{\"name\":\"Apple\",\"data\":{\"Tuesday\":1,\"Friday\":8},\"stack\":\"fruit\"},{\"name\":\"Cucumber\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"vegetable\"},{\"name\":\"Carrot\",\"data\":{\"Tuesday\":1,\"Friday\":8},\"stack\":\"vegetable\"}]"

# or apply ctx$eval() on the the list of expressions
json_lst <- lapply(js, ctx$eval, serialize = TRUE)
str(json_lst)
#> List of 2
#>  $ ex1: chr "[{\"name\":\"Orange\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"fruit\"},{\"name\":\"Apple\",\"data\":{"| __truncated__
#>  $ ex2: chr "[[\"Washington\",\"1789-04-29\",\"1797-03-03\"],[\"Adams\",\"1797-03-03\",\"1801-03-03\"]]"

<sup>Created on 2023-07-23 with reprex v2.0.2</sup>

答案2

得分: 0

为了获得一个“真正的”JSON字符串,您可以使用jsonNormalize包。

library(jsonNormalize)

jsonNormalize(
  '[
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]', prettify = TRUE)
[
  {
    "name": "Orange",
    "data": {
      "Tuesday": 3,
      "Friday": 4
    },
    "stack": "fruit"
  },
  {
    "name": "Apple",
    "data": {
      "Tuesday": 1,
      "Friday": 8
    },
    "stack": "fruit"
  },
  {
    "name": "Cucumber",
    "data": {
      "Tuesday": 3,
      "Friday": 4
    },
    "stack": "vegetable"
  },
  {
    "name": "Carrot",
    "data": {
      "Tuesday": 1,
      "Friday": 8
    },
    "stack": "vegetable"
  }
]

jsonNormalize包还提供了一个RStudio插件,允许通过点击按钮来规范化json文件。

英文:

To have a "true" JSON string, you can use the jsonNormalize package.

library(jsonNormalize)

jsonNormalize(
  &#39;[
  {name: &quot;Orange&quot;, data: {&quot;Tuesday&quot;: 3, &quot;Friday&quot;: 4}, stack: &quot;fruit&quot;},
  {name: &quot;Apple&quot;, data: {&quot;Tuesday&quot;: 1, &quot;Friday&quot;: 8}, stack: &quot;fruit&quot;},
  {name: &quot;Cucumber&quot;, data: {&quot;Tuesday&quot;: 3, &quot;Friday&quot;: 4}, stack: &quot;vegetable&quot;},
  {name: &quot;Carrot&quot;, data: {&quot;Tuesday&quot;: 1, &quot;Friday&quot;: 8}, stack: &quot;vegetable&quot;}
]&#39;, prettify = TRUE)
[
  {
    &quot;name&quot;: &quot;Orange&quot;,
    &quot;data&quot;: {
      &quot;Tuesday&quot;: 3,
      &quot;Friday&quot;: 4
    },
    &quot;stack&quot;: &quot;fruit&quot;
  },
  {
    &quot;name&quot;: &quot;Apple&quot;,
    &quot;data&quot;: {
      &quot;Tuesday&quot;: 1,
      &quot;Friday&quot;: 8
    },
    &quot;stack&quot;: &quot;fruit&quot;
  },
  {
    &quot;name&quot;: &quot;Cucumber&quot;,
    &quot;data&quot;: {
      &quot;Tuesday&quot;: 3,
      &quot;Friday&quot;: 4
    },
    &quot;stack&quot;: &quot;vegetable&quot;
  },
  {
    &quot;name&quot;: &quot;Carrot&quot;,
    &quot;data&quot;: {
      &quot;Tuesday&quot;: 1,
      &quot;Friday&quot;: 8
    },
    &quot;stack&quot;: &quot;vegetable&quot;
  }
]

The jsonNormalize package also provides a RStudio addin allowing to normalize a json file by pressing a button.

huangapple
  • 本文由 发表于 2023年7月23日 16:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定