下面是输入和输出,以及我所做的代码片段。我们可以编写一个函数来动态获取 id 并创建输出吗?
Below is the input and output, along with a code snippet of what i've done. Can we write a function to take ids dynamically and create the output?
输入
{"records" : {
"a1s1X000000YKalQAG" : {
"apiName" : "SP_Attribute__c",
"childRelationships" : { },
"eTag" : "c9439586feca3af5df2121fe7cfbc4ca",
"fields" : {
"Name" : {
"displayValue" : null,
"value" : "EuipmentRecord2"
},
"SP_Internal_Number__c" : {
"displayValue" : null,
"value" : "a"
},
"SP_Type__c" : {
"displayValue" : null,
"value" : "B"
},
"SP_Unit__c" : {
"displayValue" : null,
"value" : "C"
},
"SP_Value__c" : {
"displayValue" : null,
"value" : "D"
}
},
"id" : "a1s1X000000YKalQAG",
"lastModifiedById" : "0051X0000090wZKQAY",
"lastModifiedDate" : "2021-11-19T10:18:57.000Z",
"recordTypeId" : "012000000000000AAA",
"recordTypeInfo" : null,
"systemModstamp" : "2021-11-19T10:18:57.000Z",
"weakEtag" : 1637317137000
},
"a1s1X000000YIWLQA4" : {
"apiName" : "SP_Attribute__c",
"childRelationships" : { },
"eTag" : "fd1b7751ece0fee39101ca1e0bbb80b3",
"fields" : {
"Name" : {
"displayValue" : null,
"value" : "EuipmentRecord1"
},
"SP_Internal_Number__c" : {
"displayValue" : null,
"value" : null
},
"SP_Type__c" : {
"displayValue" : null,
"value" : null
},
"SP_Unit__c" : {
"displayValue" : null,
"value" : null
},
"SP_Value__c" : {
"displayValue" : null,
"value" : null
}
},
"id" : "a1s1X000000YIWLQA4",
"lastModifiedById" : "0051X0000090wZKQAY",
"lastModifiedDate" : "2021-11-09T12:36:22.000Z",
"recordTypeId" : "012000000000000AAA",
"recordTypeInfo" : null,
"systemModstamp" : "2021-11-09T12:36:22.000Z",
"weakEtag" : 1636461382000
}
}}
Dataweave
%dw 2.0
output application/json
var ids = ["a1s1X000000YKalQAG", "a1s1X000000YIWLQA4"]
var a = [payload.records."$(ids[0])".fields pluck (($$): $.value)]++ [(payload.records."$(ids[1])".fields pluck (($$): $.value))]
---
a
输出
[
[
{
"Name": "EuipmentRecord2"
},
{
"SP_Internal_Number__c": "a"
},
{
"SP_Type__c": "B"
},
{
"SP_Unit__c": "C"
},
{
"SP_Value__c": "D"
}
],
[
{
"Name": "EuipmentRecord1"
},
{
"SP_Internal_Number__c": null
},
{
"SP_Type__c": null
},
{
"SP_Unit__c": null
},
{
"SP_Value__c": null
}
]
]
保持输出相同,挑战在于编写一个动态获取这些 ID 并创建输出的函数,因此在此示例中有两个 ID -“a1s1X000000YKalQAG”、“a1s1X000000YIWLQA4”,因此变量“a”有两个数组要连接,[payload.records."$(ids[0])".fields pluck (($$): $.value)]++ [(payload.records."$( ids[1])".fields 采摘 (($$): $.value))].
我们可以写一个函数让整个事情动态化吗?所以如果有 n 个 id,-> id1 ++ id2 ++ id3... id(n) ?
ID 似乎是记录值的键,因此您可以只使用 namesOf() 函数将键提取为字符串列表,以避免在脚本中对其进行硬编码:
%dw 2.0
output application/json
fun convertById(data) =
payload.records
pluck ($.fields)
map ($ pluck (($$): $.value) )
---
convertById(payload)
更新:封装成一个函数。
Update2:让它对任意数量的 id 都是动态的。
输入
{"records" : {
"a1s1X000000YKalQAG" : {
"apiName" : "SP_Attribute__c",
"childRelationships" : { },
"eTag" : "c9439586feca3af5df2121fe7cfbc4ca",
"fields" : {
"Name" : {
"displayValue" : null,
"value" : "EuipmentRecord2"
},
"SP_Internal_Number__c" : {
"displayValue" : null,
"value" : "a"
},
"SP_Type__c" : {
"displayValue" : null,
"value" : "B"
},
"SP_Unit__c" : {
"displayValue" : null,
"value" : "C"
},
"SP_Value__c" : {
"displayValue" : null,
"value" : "D"
}
},
"id" : "a1s1X000000YKalQAG",
"lastModifiedById" : "0051X0000090wZKQAY",
"lastModifiedDate" : "2021-11-19T10:18:57.000Z",
"recordTypeId" : "012000000000000AAA",
"recordTypeInfo" : null,
"systemModstamp" : "2021-11-19T10:18:57.000Z",
"weakEtag" : 1637317137000
},
"a1s1X000000YIWLQA4" : {
"apiName" : "SP_Attribute__c",
"childRelationships" : { },
"eTag" : "fd1b7751ece0fee39101ca1e0bbb80b3",
"fields" : {
"Name" : {
"displayValue" : null,
"value" : "EuipmentRecord1"
},
"SP_Internal_Number__c" : {
"displayValue" : null,
"value" : null
},
"SP_Type__c" : {
"displayValue" : null,
"value" : null
},
"SP_Unit__c" : {
"displayValue" : null,
"value" : null
},
"SP_Value__c" : {
"displayValue" : null,
"value" : null
}
},
"id" : "a1s1X000000YIWLQA4",
"lastModifiedById" : "0051X0000090wZKQAY",
"lastModifiedDate" : "2021-11-09T12:36:22.000Z",
"recordTypeId" : "012000000000000AAA",
"recordTypeInfo" : null,
"systemModstamp" : "2021-11-09T12:36:22.000Z",
"weakEtag" : 1636461382000
}
}}
Dataweave
%dw 2.0
output application/json
var ids = ["a1s1X000000YKalQAG", "a1s1X000000YIWLQA4"]
var a = [payload.records."$(ids[0])".fields pluck (($$): $.value)]++ [(payload.records."$(ids[1])".fields pluck (($$): $.value))]
---
a
输出
[
[
{
"Name": "EuipmentRecord2"
},
{
"SP_Internal_Number__c": "a"
},
{
"SP_Type__c": "B"
},
{
"SP_Unit__c": "C"
},
{
"SP_Value__c": "D"
}
],
[
{
"Name": "EuipmentRecord1"
},
{
"SP_Internal_Number__c": null
},
{
"SP_Type__c": null
},
{
"SP_Unit__c": null
},
{
"SP_Value__c": null
}
]
]
保持输出相同,挑战在于编写一个动态获取这些 ID 并创建输出的函数,因此在此示例中有两个 ID -“a1s1X000000YKalQAG”、“a1s1X000000YIWLQA4”,因此变量“a”有两个数组要连接,[payload.records."$(ids[0])".fields pluck (($$): $.value)]++ [(payload.records."$( ids[1])".fields 采摘 (($$): $.value))].
我们可以写一个函数让整个事情动态化吗?所以如果有 n 个 id,-> id1 ++ id2 ++ id3... id(n) ?
ID 似乎是记录值的键,因此您可以只使用 namesOf() 函数将键提取为字符串列表,以避免在脚本中对其进行硬编码:
%dw 2.0
output application/json
fun convertById(data) =
payload.records
pluck ($.fields)
map ($ pluck (($$): $.value) )
---
convertById(payload)
更新:封装成一个函数。 Update2:让它对任意数量的 id 都是动态的。