简体   繁体   中英

Dataweave 2.0 Mapping transformation

I have an input of the following payload for a POST method:

{
  "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:

{
   "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.

%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
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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