简体   繁体   English

Dataweave 2.0 映射转换

[英]Dataweave 2.0 Mapping transformation

I have an input of the following payload for a POST method:对于 POST 方法,我输入了以下有效负载:

{
  "order": {
    "Column_X": "X",
    "Column_Y": "Y",
    "Column_Z": "Z",
    "Column_W" : {
      "div_1": "some text",
      "div_2": true,
      "div_3": 2
    }
  },
  "mapper": {
    "A": "<order.Column_X>",
    "B": "<order.Column_Z>",
    "C": "Fields or text not mapped to order",
    "C1": "status",
    "D": {
      "D1": "<order.Column_W.div_1>",
      "D2": "<order.Column_W.div_2>",
      "D3": "<order.Column_W.div_3>",
      "D4": "<order.Column_W.div_4>"
    }
  }
}

Here is the expected output mapping with the order object above:这是预期的 output 映射,上面的顺序为 object:

{
   "A":"X",
   "B":"Z",
   "C":"Fields or text not mapped to order",
   "C1":"status",
   "D":{
      "D1":"some text",
      "D2":true,
      "D3":2,
      "D4":null
   }
}

How would I approach this to solve it?我将如何解决这个问题?

I hope this is what you are looking for.我希望这就是你要找的。

%dw 2.0
output application/json
---
{
    A: payload.order.Column_X,
    B: payload.order.Column_Z,
    C: payload.mapper.C,
    C: payload.mapper.C1,
    D: {
        D1: payload.order.Column_W.div_1,
        D2: payload.order.Column_W.div_2,
        D3: payload.order.Column_W.div_3,
        D4: payload.order.Column_W.div_4
    }
}

Output:
{
  "A": "X",
  "B": "Z",
  "C": "Fields or text not mapped to order",
  "C": "status",
  "D": {
    "D1": "some text",
    "D2": true,
    "D3": 2,
    "D4": null
  }
}

This is bit crude approach though hopefully it gives you an idea to query the "order" object a bit dyamically.这是有点粗略的方法,但希望它能给你一个想法来查询“订单” object 有点动态。

%dw 2.0
output application/json
fun returnFromOrder(val : Array) = {
  a:  if (sizeOf(val) ==2) payload."$(val[0])"."$(val[1])"  else payload."$(val[0])"."$(val[1])"."$(val[2])"
}

---
{
    A: returnFromOrder(( payload.mapper.A replace "<" with "" replace ">" with "" splitBy ".")).a,
    B: returnFromOrder(( payload.mapper.B replace "<" with "" replace ">" with "" splitBy ".")).a,
    C: if (returnFromOrder(( payload.mapper.C replace "<" with "" replace ">" with "" splitBy ".")).a == null) payload.mapper.C else "",
    C1: if (returnFromOrder(( payload.mapper.C1 replace "<" with "" replace ">" with "" splitBy ".")).a == null) payload.mapper.C1 else "",
    D: {
        "D1" : returnFromOrder(( payload.mapper.D.D1 replace "<" with "" replace ">" with "" splitBy ".")).a,
        "D2" : returnFromOrder(( payload.mapper.D.D2 replace "<" with "" replace ">" with "" splitBy ".")).a,
        "D3" : returnFromOrder(( payload.mapper.D.D3 replace "<" with "" replace ">" with "" splitBy ".")).a,
        "D4" : returnFromOrder(( payload.mapper.D.D4 replace "<" with "" replace ">" with "" splitBy ".")).a
    }

}

To make it fully dynamic you need two things.要使其完全动态化,您需要两件事。

  1. A function that can fetch a value from a dynamic path可以从动态路径中获取值的 function
  2. Use recursion to build the payload since your mapper itself can contain more Objects that are mapper.使用递归来构建有效负载,因为您的映射器本身可以包含更多的映射器对象。
%dw 2.0
output application/json

fun isPath(value) = (value is String) and (value matches "<.+>")

fun getFromPath(json, pathString) = 
    pathString splitBy '.'
        reduce ((path, valueAtPath = json) -> valueAtPath[path])

fun buildUsingMapper(mapper, mappingValues) = 
mapper mapObject ((value, key) -> {
    (key): if(isPath(value)) mappingValues getFromPath value[1 to -2] // eliminate the first "<" and the ending ">". 
            else if(value is Object) buildUsingMapper(value, mappingValues)
            else value
})
---
buildUsingMapper(payload.mapper, payload - "mapper") // you can also pass the full payload instead of payload - "mapper". I did it here to show that the second parameter does not necessarily need the mapper object within.

I have assumed there are no arrays that will have these dynamic path in mapper , Or the dynamic paths will include accessing an array element dynamically.我假设没有 arrays 在mapper中有这些动态路径,或者动态路径将包括动态访问数组元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM