英文:
How to use JSONata to change JSON response restructure
问题
{
"output": [
{
"enterpriseid": 53898,
"enterprise": "Absolute F and B Facilities Management LLC",
"bookingvalue": 520.5
},
{
"enterpriseid": 53899,
"enterprise": "testing Buyer",
"bookingvalue": 200
}
]
}
英文:
I have one JSON data that I want to restructure the data using JSONata query
{
"response": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 46
},
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 275
},
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 199.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer ",
"bookingvalue": 200
}
]
}
And I'm trying to use JSONata to change it to something more like this:
{
"output": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 520.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer",
"bookingvalue": 200
}
]
}
i tried with this query but i am getting only enterpriseid and enterprise not getting bookingvalue
{
"output": $distinct(response.{
"enterpriseid" : enterpriseid,
"enterprise": enterprise,
"bookingvalue": $sum($filter(response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
} )
}
答案1
得分: 0
您需要使用双美元符号($$
)来跳出$distinct
的上下文,以对所有筛选项的总和进行计算。
$$
输入JSON的根。只有在需要暂时导航到不同路径时才需要。
例如:
{
"output": $distinct(response.{
"enterpriseid": enterpriseid,
"enterprise": enterprise,
"bookingvalue": $sum($filter($$.response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
})
}
(请注意,在$filter
内部使用了双美元符号$$
。)
这将输出:
{
"output": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 520.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer ",
"bookingvalue": 200
}
]
}
工作示例: https://try.jsonata.org/Y-7YTZ_eJ
英文:
You need to use double dollar signs ($$
) to break out of $distinct
's context for the sum of all filtered items.
> $$
The root of the input JSON. Only needed if you need to break out of
> the current context to temporarily navigate down a different path.
For example:
{
"output": $distinct(response.{
"enterpriseid" : enterpriseid,
"enterprise": enterprise,
"bookingvalue": $sum($filter($$.response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
})
}
(Note the double dollar sign $$
usage inside of $filter
.)
Which outputs:
{
"output": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 520.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer ",
"bookingvalue": 200
}
]
}
Working Example: https://try.jsonata.org/Y-7YTZ_eJ
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论