英文:
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(
'[
{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"
}
]
The jsonNormalize package also provides a RStudio addin allowing to normalize a json file by pressing a button.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论