使用 Dataweave 如何将输入 Json 转换为以下输出?

Using Dataweave how to convert the input Json into the following output?

输入

{"records" : {
    "a1e1X00000Ly5GfQAJ" : {
      "apiName" : "SP_Equipment__c",
      "childRelationships" : { },
      "eTag" : "488cbd9fb3c67e20dbaac386ee2a49aa",
      "fields" : {
        "RecordType" : {
          "displayValue" : "Product",
          "value" : {
            "apiName" : "RecordType",
            "childRelationships" : { },
            "eTag" : "69f1f78c0422f456b554c7e2f2f481c0",
            "fields" : {
              "Id" : {
                "displayValue" : null,
                "value" : "0121X0000036fjzQAA"
              },
              "Name" : {
                "displayValue" : "Product",
                "value" : "Product"
              }
            },
            "id" : "0121X0000036fjzQAA",
            "lastModifiedById" : "00509000002pidBAAQ",
            "lastModifiedDate" : "2021-10-29T08:29:32.000Z",
            "recordTypeId" : null,
            "recordTypeInfo" : null,
            "systemModstamp" : "2021-10-29T08:29:32.000Z",
            "weakEtag" : 1635496172000
          }
        },
        "RecordTypeId" : {
          "displayValue" : null,
          "value" : "0121X0000036fjzQAA"
        },
        "SP_End_of_Commissioning__c" : {
          "displayValue" : null,
          "value" : null
        },
        "SP_End_of_External_Warranty__c" : {
          "displayValue" : null,
          "value" : null
        },
        "SP_Main_Product_Group__c" : {
          "displayValue" : null,
          "value" : "a1j1X000001opfqQAA"
        },
        "SP_Main_Product_Group__r" : {
          "displayValue" : "Motors HV Loher Tube Cooled (Ex flameproof)",
          "value" : {
            "apiName" : "SP_Product_Group__c",
            "childRelationships" : { },
            "eTag" : "3e30fe6b4f1ced0d9d31e9b188ea1ad9",
            "fields" : {
              "Id" : {
                "displayValue" : null,
                "value" : "a1j1X000001opfqQAA"
              },
              "Name" : {
                "displayValue" : null,
                "value" : "Motors HV Loher Tube Cooled (Ex flameproof)"
              }
            },
            "id" : "a1j1X000001opfqQAA",
            "lastModifiedById" : "0051X0000093craQAA",
            "lastModifiedDate" : "2021-10-07T08:10:14.000Z",
            "recordTypeId" : "012000000000000AAA",
            "recordTypeInfo" : null,
            "systemModstamp" : "2021-10-07T08:10:14.000Z",
            "weakEtag" : 1633594214000
          }
        },
        "SP_Number__c" : {
          "displayValue" : null,
          "value" : "10186735"
        },
        "SP_Product_Number__c" : {
          "displayValue" : null,
          "value" : "1MV4567-6BJ60-4BD0-Z"
        },
        "SP_Serial_Number__c" : {
          "displayValue" : null,
          "value" : "LDX/50000479"
        }
      },
      "id" : "a1e1X00000Ly5GfQAJ",
      "lastModifiedById" : "0051X000008s3oDQAQ",
      "lastModifiedDate" : "2021-11-08T13:39:23.000Z",
      "recordTypeId" : "0121X0000036fjzQAA",
      "recordTypeInfo" : {
        "available" : true,
        "defaultRecordTypeMapping" : true,
        "master" : false,
        "name" : "Product",
        "recordTypeId" : "0121X0000036fjzQAA"
      },
      "systemModstamp" : "2021-11-08T13:39:23.000Z",
      "weakEtag" : 1636378763000
    }
 }}

我试过的

%dw 2.0
output application/json
import last from dw::core::Strings
var values = payload.records.a1e1X00000Ly5GfQAJ.fields 
var keypair = values mapObject (item, index, mapper)->{
     "$(index)": if(("$(index)" last 3) == "__c") item.value else if("$(index)"  == "recordType") item.value else item.displayValue
}

---
keypair 

当前输出

{
  "RecordType": "Product",
  "RecordTypeId": null,
  "SP_End_of_Commissioning__c": null,
  "SP_End_of_External_Warranty__c": null,
  "SP_Main_Product_Group__c": "a1j1X000001opfqQAA",
  "SP_Main_Product_Group__r": "Motors HV Loher Tube Cooled (Ex flameproof)",
  "SP_Number__c": "10186735",
  "SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z",
  "SP_Serial_Number__c": "LDX/50000479"
}

需要输出

{
  "RecordTypeId": "Product",
  "SP_End_of_Commissioning__c": null,
  "SP_End_of_External_Warranty__c": null,
  "SP_Main_Product_Group__c": "Motors HV Loher Tube Cooled (Ex flameproof)",
  "SP_Number__c": "10186735",
  "SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z",
  "SP_Serial_Number__c": "LDX/50000479"
}

这可以是动态的,假设有一个字段 abcd__c 并且它有一个相应的 abcd__r 字段并且必须从 abcd__r 中获取值以形成这些情况的输出.例如:

如果输入是

{ 
  "abcd__c": "A001",
  "abcd__r": "A002"
} 

输出

{ "abcd__c": "A002" }

要求和示例不明确,因此我采用了一些假设来创建此脚本:

%dw 2.0
output application/json
import last from dw::core::Strings
fun replaceCwithR(s)=s replace /__c/ with("__r")
var values = payload.records.a1e1X00000Ly5GfQAJ.fields 
fun convertRtoC(o) = o 
    mapObject ((item, key, index) -> {
        "$(key as String)":  
            if(("$(key as String)" last 3) == "__c") 
                if (!isEmpty(values[replaceCwithR(key as String)]))
                    log(values[replaceCwithR(key as String)].displayValue)
                else item.value
            else 
                if("$(key as String)" == "RecordTypeId") values.RecordType.displayValue 
                else item.displayValue
        }
    )
    filterObject ((item, key, index)-> ((key as String) last 3) != "__r" and (key as String != "RecordType"))
---
convertRtoC(values)

输出(对于提供的输入:

{
  "RecordTypeId": "Product",
  "SP_End_of_Commissioning__c": null,
  "SP_End_of_External_Warranty__c": null,
  "SP_Main_Product_Group__c": "Motors HV Loher Tube Cooled (Ex flameproof)",
  "SP_Number__c": "10186735",
  "SP_Product_Number__c": "1MV4567-6BJ60-4BD0-Z",
  "SP_Serial_Number__c": "LDX/50000479"
}