从给定的 json 中,我想获得所需的输出,如何使用 dataweave 来实现?
From the given json, I want to get the desired output, How to do it using dataweave?
输入
{
"editable": true,
"sections": [
{
"title": "Identification",
"Content": [
{
"name": "Classification",
"apiname": "SP_Classification__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "Product Number",
"apiname": "SP_Product_Number__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "Current Equipment Partner",
"apiname": "SP_Current_Equipment_Partner__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "End of Commissioning",
"apiname": "SP_End_of_Commissioning__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
}
]
},
{
"title": "Position And Contact",
"Content": [
{
"name": "Batch Number",
"apiname": "SP_Batch_Number__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "Altitude",
"apiname": "SP_Altitude__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
}
]
}
]
}
输出
{
"editable": true,
"sections": [
{
"title": "Identification",
"Content": [
{
"name": "Classification",
"apiname": "SP_Classification__c",
"text": "Products"
"url": "",
"info": ""
},
{
"name": "Product Number",
"apiname": "SP_Product_Number__c",
"text": "1MV4567-6BJ60-4BD0-Z",
"url": "",
"info": ""
},
{
"name": "Current Equipment Partner",
"apiname": "SP_Current_Equipment_Partner__c",
"text": "E001,
"url": "",
"info": ""
},
{
"name": "End of Commissioning",
"apiname": "SP_End_of_Commissioning__c",
"text": "End001",
"url": "",
"info": ""
}
]
},
{
"title": "Position And Contact",
"Content": [
{
"name": "Batch Number",
"apiname": "SP_Batch_Number__c",
"text": "B001,
"url": "",
"info": ""
},
{
"name": "Altitude",
"apiname": "SP_Altitude__c",
"text": "A001",
"url": "",
"info": ""
}
]
}
您似乎需要按原样映射每个属性,但 Content 元素中的 text
属性除外。那些可以用 filterObject()
过滤
%dw 2.0
output application/json
---
{
editable: payload.editable,
sections: payload.sections map
{
title: $.title,
Content: $.Content map
($ mapObject ((value, key, index) ->
(key): if (key as String != "text") value
else value[$.apiname]
)
)
}
}
输入
{
"editable": true,
"sections": [
{
"title": "Identification",
"Content": [
{
"name": "Classification",
"apiname": "SP_Classification__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "Product Number",
"apiname": "SP_Product_Number__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "Current Equipment Partner",
"apiname": "SP_Current_Equipment_Partner__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "End of Commissioning",
"apiname": "SP_End_of_Commissioning__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
}
]
},
{
"title": "Position And Contact",
"Content": [
{
"name": "Batch Number",
"apiname": "SP_Batch_Number__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
},
{
"name": "Altitude",
"apiname": "SP_Altitude__c",
"text": {
"Name": "EQ-10186735",
"SP_Altitude__c": "A001",
"SP_Batch_Number__c": "B001",
"SP_Classification__c": "Products",
"SP_Current_Equipment_Partner__c": "E001",
"SP_Current_Equipment_Partner__r": "E002",
"SP_Delivery_Date__c": "2016-09-02",
"SP_Delivery_Note__c": "82849654",
"SP_End_of_Commissioning__c": "End001",
"SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z"
},
"url": "",
"info": ""
}
]
}
]
}
输出
{
"editable": true,
"sections": [
{
"title": "Identification",
"Content": [
{
"name": "Classification",
"apiname": "SP_Classification__c",
"text": "Products"
"url": "",
"info": ""
},
{
"name": "Product Number",
"apiname": "SP_Product_Number__c",
"text": "1MV4567-6BJ60-4BD0-Z",
"url": "",
"info": ""
},
{
"name": "Current Equipment Partner",
"apiname": "SP_Current_Equipment_Partner__c",
"text": "E001,
"url": "",
"info": ""
},
{
"name": "End of Commissioning",
"apiname": "SP_End_of_Commissioning__c",
"text": "End001",
"url": "",
"info": ""
}
]
},
{
"title": "Position And Contact",
"Content": [
{
"name": "Batch Number",
"apiname": "SP_Batch_Number__c",
"text": "B001,
"url": "",
"info": ""
},
{
"name": "Altitude",
"apiname": "SP_Altitude__c",
"text": "A001",
"url": "",
"info": ""
}
]
}
您似乎需要按原样映射每个属性,但 Content 元素中的 text
属性除外。那些可以用 filterObject()
%dw 2.0
output application/json
---
{
editable: payload.editable,
sections: payload.sections map
{
title: $.title,
Content: $.Content map
($ mapObject ((value, key, index) ->
(key): if (key as String != "text") value
else value[$.apiname]
)
)
}
}