[英]How to get value by comparing it with index of another field in the array of objects
If the text we are looking for is "PQR" then we should get the corresponding subfield value "dataweave".如果我们要查找的文本是“PQR”,那么我们应该得到相应的子字段值“dataweave”。 In the first object, we have the value but in the second object that is missing so it should give default null.在第一个对象中,我们有值,但在第二个对象中丢失了,所以它应该给默认 null。 Also the text will not be in order.文本也将不按顺序排列。 "ABC","BCD","PQR" are static in output even though it might come or not come in the input. “ABC”、“BCD”、“PQR”在输出中是静态的,即使它可能会出现或不会出现在输入中。
Input:输入:
[ {
"data":{
"field": "value"
},
"field1": "ABC",
"subfield01": "test",
"field2": "BCD",
"subfield02": "mule",
"field3": "PQR",
"subfield3": "dataweave"
.
.
.
"field10":"",
"subfield10": ""
},
{
"data":{
"field": "value"
}
"field1": "BCD",
"subfield01": "testing",
"field2": "ABC",
"subfield02": "mulesoft",
"field3": "",
"subfield03": ""
.
.
.
"field10":"",
"subfield10": ""
}
]
Expected Output:预期输出:
[
{
"SearchFieldABC": "test",
"SearchFieldBCD": "mule",
"SearchFieldPQR": "dataweave",
"fields123": "value",
"fields678": "value2"
},
{
"SearchFieldABC": "mulesoft",
"SearchFieldBCD": "testing",
"SearchFieldPQR": null,
"fields123": "value",
"fields678": "value2"
}
]
Though previous answer looks ok I post my虽然以前的答案看起来不错,但我发布了我的
%dw 2.0
output application/json
var query = ["ABC", "BCD", "PQR"]
---
payload map ((item, index) -> do { //Group by index
var fieldByIndex = item
groupBy ((value, key) -> do {
key as String match {
case f if(f startsWith "field") -> f[5 to -1]
case f if(f startsWith "subfield") -> f[8 to -1]
}
})
---
{
(
query
map ((q, index) -> do {
var fieldSubField = (fieldByIndex
filterObject ((value, key, index) -> value['field$(key)'] == q))[0]
---
{
"SearchField$(q)": valuesOf(
fieldSubField
filterObject ((value, key, index) -> key as String startsWith "subfield"))[0]
}
})
)
}
} )
You can use the following dataweave.您可以使用以下数据编织。 I have divided it into two main functions, first mapFieldsToSubFields
takes an element from your input and basically generates an object that directly maps the field's values to their respective subfield's value.我将它分为两个主要功能,第一个mapFieldsToSubFields
从您的输入中获取一个元素,基本上生成一个对象,该对象直接将字段的值映射到它们各自的子字段的值。 So it will generate the following output for example因此它将生成以下输出,例如
{
"ABC": "test",
"BCD": "mule",
"PQR": "dataweave"
}
This will make it is easier for the next function mapToRequiredOutput
to quickly map it to required format and insert the missing fields from the input.这将使下一个函数mapToRequiredOutput
更容易将其快速映射到所需格式并从输入中插入缺失的字段。
%dw 2.0
output application/json
var allFields = payload flatMap entriesOf($)
filter ($.key startsWith "field") and (!isEmpty($.value))
map $.value
distinctBy $
fun mapFieldsToSubFields(object) =
object filterObject ((value, key) -> (key startsWith "field")) // Remove all "subfields"
filterObject !isEmpty($) // Remove empty field values
mapObject ($): object["sub$($$)"] // example: $ = "ABC" and $$ = field1, so object["sub$($$)"] = object[subfield1] = "test"
fun mapToRequiredOutput(object) = {(
allFields map {"SearchField$($)": object[$]}
)}
---
payload
map mapFieldsToSubFields($)
map mapToRequiredOutput($)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.