我有这样的JSON:
{
"dcsId": "1184001100000000517",
"marketCode": "US",
"languageCode": "en-US",
"profile": {
"base": {
"username": "arunima27",
"activeInd": "R",
"phone": [
{
"activeInd": "Y",
"type": "mobile",
"primaryInd": "Y",
"number": "2234566788"
},
{
"activeInd": "N",
"type": "mobile",
"primaryInd": "N",
"number": ""
}
]
}
}
}
从这个输入JSON我们需要提取payload.profile.base.phone.number,其中payload.profile.base.phone.type ==“mobile”和payload.profile.base.phone.activeInd ==“ Y”。实际上我们需要循环遍历JSON数组(payload.profile.base。* phone)并仅获取活动且类别/类型为移动的电话号码。
我们需要如下输出:
{
"dcsId": "1184001100000000517",
"marketCode": "US",
"languageCode": "en-US",
"username" : "arunima27",
"phoneNumber" : "2234566788"
}
我们在为“phoneNumber”输出变量进行此转换时遇到问题。
答案 0 :(得分:6)
从您的预期结果看起来您只想要第一个匹配的数字。这是数据编织正是这样做的:
%dw 1.0
%output application/json
---
{
dcsid: payload.dcsId,
markCode: payload.marketCode,
languageCode: payload.languageCode,
username: payload.profile.base.username,
phoneNumber: (payload.profile.base.phone filter ($.activeInd == 'Y' and $.type == 'mobile'))[0].number
}
如果您想要所有匹配的电话号码,只需离开[0]
,结果中的phoneNumber
值将是一个数组(而不仅仅是第一个匹配的电话号码)。
答案 1 :(得分:4)
这解决了这个问题。
{(payload.profile.base.phone filter($ .activeInd ==" Y" and $ .primaryInd ==" Y"))}。number default&#34 ;"
答案 2 :(得分:1)
在手机上使用过滤器和地图,以达到理想的效果。
phoneNumber: {
(payload.profile.base.phone filter ($.activeInd == "Y" and $.type == "mobile") map {
number: $.number
}
)}
<强>输出强>
"phoneNumber": {
"number": "2234566788",
}
答案 3 :(得分:-2)
%dw 2.0
output application/json
---
payload.profile.base.phone filter ((obj, index) -> (obj.activeInd == 'Y' and obj.type ~= 'mobile' ))