0

我有一些 JSON 数组作为 -

"expenseRecords": [ { "date_expensed": "2019-04-01", "exp_cat_description": "Air Travel", "totalamountUSD": 10, "totalRecords": 2 }, { "date_expensed": "2019-04- 01”,“exp_cat_description”:“早餐”,“totalamountUSD”:11,“totalRecords”:1 }]

我想要的格式是 "expenseRecords": [ { "date_expensed": "2019-04-01",{ "exp_cat_description": "Air Travel", "totalamountUSD": 10, "totalRecords": 2 }, { "exp_cat_description" : "早餐", "totalamountUSD": 11, "totalRecords": 1 }, ]

如何在 R 中实现这一点

4

2 回答 2

0
let newarray =[]

const object1 = {
  name: 'Flavio'
}

const object2 = {
  age: 35
}

const object3 = {...object1, ...object2 }
newArray=[...newArray,object3]
于 2019-06-04T10:37:26.663 回答
0

您可以使用 jsonlite 库和 split data.frame 函数。

首先将您的 json 转换为 R 对象,然后通过变量拆分 data.frame,然后重写 json 对象。

inputjson <- '{"expenseRecords": [ { "date_expensed": "2019-04-01", "exp_cat_description": "Air Travel", "totalamountUSD": 10, "totalRecords": 2 }, { "date_expensed": "2019-04-01", "exp_cat_description": "Breakfast", "totalamountUSD": 11, "totalRecords": 1 },{ "date_expensed": "2019-04-02", "exp_cat_description": "Air Travel", "totalamountUSD": 10, "totalRecords": 2 }]}'

library(jsonlite)

input <- fromJSON(inputjson)

input$expenseRecords <- split(input$expenseRecords[,-1], input$expenseRecords$date_expensed)

output <- toJSON(input)
于 2019-06-07T13:32:50.263 回答