我有以下输入,并且需要使所有对象在输出中单独出现,如下所示:
我的输入:
{
"factPanel": {
"nutrition": {
"columns": [
{
"name": "Per Serving",
"nutrients": [
{
"name": "Calories",
"valueOperator": "=",
"value": 170,
"uom": "kcal",
"percentDvOperator": "=",
"percentDv": 12
},
{
"name": "Saturated Fat",
"valueOperator": "=",
"value": 0,
"uom": "g",
"percentDvOperator": "=",
"percentDv": 0
}
]
},
{
"name": "Per Recommended Serving",
"nutrients": [
{
"name": "Calories",
"valueOperator": "=",
"value": 210,
"uom": "kcal",
"percentDvOperator": "=",
"percentDv": 13
}
]
}
]
}
}
}
所需的正确输出:
{
"li_nutrients" : {
"values" : [ {
"nutrientPercentageDailyValue" : 12,
"nutrientValue" : "170 kcal",
"nutrientName" : "Calories"
}, {
"nutrientPercentageDailyValue" : 0,
"nutrientValue" : "0 g",
"nutrientName" : "Saturated Fat"
},
{
"nutrientPercentageDailyValue" : 13,
"nutrientValue" : "210 kcal",
"nutrientName" : "Calories"
}]
}
}
但是无论我在规范中进行什么更改,我只能实现以下目标:
{
"li_nutrients" : {
"values" : [ {
"nutrientPercentageDailyValue" : [ 12, 13 ],
"nutrientValue" : [ 170, "kcal", 210, "kcal" ],
"nutrientName" : [ "Calories", "Calories" ]
}, {
"nutrientPercentageDailyValue" : 0,
"nutrientValue" : [ 0, "g" ],
"nutrientName" : "Saturated Fat"
} ]
}
}
我写的规范:
[
{
"operation": "shift",
"spec": {
"factPanel": {
"nutrition": {
"columns": {
"*": {
"nutrients": {
"*": {
"name": {
"*": {
"@(2,name)": "li_nutrients.values[&3].nutrientName",
"@(2,percentDvOperator)": {
"=": {
"@(4,percentDv)": "li_nutrients.values[&5].nutrientPercentageDailyValue"
},
"*": {
"@(4,percentDvOperator)|@(4,percentDv)": "li_nutrients.values[&5].nutrientPercentageDailyValue"
}
},
"@(2,valueOperator)": {
"=": {
"@(4,value)|@(4,uom)": "li_nutrients.values[&5].nutrientValue"
},
"*": {
"@(4,valueOperator)|@(4,value)|@(4,uom)": "li_nutrients.values[&5].nutrientValue"
}
}
}
}
}
}
}
}
}
}
}
}
]
有人可以解释一下我在这里做错了什么吗?上述颠簸规格需要进行哪些更改?预先感谢!
您应该添加两个分层分区以更精细地区分元素,例如:
[
{//partition the stuff by the indexes of "nutrients" and "columns" arrays concurrently
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"nutrients": {
"*": {
"percentDv": "li_&2_&3_&1.nutrientPercentageDailyValue",
"value|uom": "li_&2_&3_&1.nutrientValue",
"name": "li_&2_&3_&1.nutrientName"
}
}
}
}
}
}
}
},
{//convert arrays to simple strings(in this case "nutrientValue" is considered)
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=join(' ',@(1,&))"
}
}
},
{//add object and aray wrappers
"operation": "shift",
"spec": {
"*_*_*_*": {
"*": "&(1,1)_&(1,2).values[#2].&"
}
}
}
]