我有一个类似的扁平JSON文件,我想为“Material_name_lv1”,“Material_name_lv2”和“Material_name_lv3”创建一个嵌套字段。
[
{
"Vendor": 100000,
"Vendor.Name": "xxxCARS",
"PO.Number": 4100004621,
"Prod.Desc": "Taxi",
"Date": "04/01/2014",
"Material.group": "CO1250106",
"Material.Name": "Corporate - Travel - Taxis",
"PO.Item": 1,
"Requester": "65",
"Requester.Name": "Anna",
"Payment.Terms": "YT",
"Order..Value": 100,
"GR..Value": 0,
"GR..": 0,
"Invoiced..Amount": 100,
"Invoiced..": 100,
"material_name_lv1": "Corporate ",
"material_name_lv2": " Travel ",
"material_name_lv3": " Taxis"
},
{
"Vendor": 100000,
"Vendor.Name": "xxx CARS",
"PO.Number": 4100011066,
"Prod.Desc": "Taxi when and as required",
"Date": "02/01/2015",
"Material.group": "CO1250106",
"Material.Name": "Corporate - Travel",
"PO.Item": 1,
"Requester": "65",
"Requester.Name": "Anna",
"Payment.Terms": "YT",
"Order..Value": 500,
"GR..Value": 0,
"GR..": 0,
"Invoiced..Amount": 500,
"Invoiced..": 100,
"material_name_lv1": "Corporate ",
"material_name_lv2": " Travel ",
"material_name_lv3": ""
}
]
我希望“Material_name_lv3”成为“Material_name_lv2”和“Material_name_lv2”的嵌套字段,成为“Material_name_lv1”的嵌套字段。
例如: -
{
"Vendor": 100000,
"Vendor.Name": "500 CARS",
"PO.Number": 4100004621,
"Prod.Desc": "Taxi",
"Date": "04/01/2014",
"Material.group": "CO1250106",
"Material.Name": "Corporate - Travel - Taxis",
"PO.Item": 1,
"Requester": "65",
"Requester.Name": "Kurzawa, Anna",
"Payment.Terms": "YP30",
"Order..Value": 10000,
"GR..Value": 0,
"GR..": 0,
"Invoiced..Amount": 10000,
"Invoiced..": 100,
"material_name_lv1":{
"name" : "coperate",
"material_name_lv2": {
"name": "Travel",
material_name_lv3": {
"name": "Taxis"
}
},
},
},
我是R的新手,从未使用过JSON文件。我发现了一堆处理JSON的R包,但在格式化命令方面没有任何实用性。任何指导将不胜感激。
注意: - 我从“Material.Name”拆分了material_name级别,某些行只包含2个级别。
由于
答案 0 :(得分:0)
您只需要创建一个与JSON输出相同的表单...
mylist <- list(a="something",
b=list("name"="myname",
"material_name"=list("name"="mat_name",
"age"=12
)
)
)
> jsonlite::toJSON(mylist, pretty=TRUE, auto_unbox=TRUE)
{
"a": "something",
"b": {
"name": "myname",
"material_name": {
"name": "mat_name",
"age": 12
}
}
}