简体   繁体   中英

Transformation of Payload to custom object using DataWeave 2.0

I have an input payload (json array) that needs to be enriched with a custom format.

Input payload:

{
  "report": [
    {
      "totPills": 100,
      "food": "chm",
      "drink": "kgf",
      "202201": 1,
      "202202": null,
      "202203": 0.39
    },
    {
      "totPills": 100,
      "food": "mkm",
      "drink": "wfl",
      "202201": 0.0,
      "202202": 1.36,
      "202203": 0
    }
  ]
}

In above payload properties 202201 , 202202 , 202203 are dynamic properties.

Expected output:

{
  "totPills": 100,
  "report": [
    {
      "food": "chm",
      "drink": "kgf",
      "qty": {
        "202201": 1,
        "202202": null,
        "202203": 0.39
      }
    },
    {
      "food": "mkm",
      "drink": "wfl",
      "qty": {
        "202201": 0.0,
        "202202": 1.36,
        "202203": 0
      }
    }
  ]
}

I tried following Script to get above results (it does not provide expected result):

%dw 2.0
import * from dw::core::Strings
output application/json

var dynWeek = "'202201','202202','202203'"

var pl = payload.report default[] map ((item, index) -> item - 'totPills' )
var pl1 = pl default [] map(($ default {}) mapObject ((value, key, index) -> {
    ((key):value) if ((dynWeek contains((key as String))) == false)
}) )
var pl2 = pl default [] map(($ default {}) mapObject ((value, key, index) -> {
    ((key):value) if ((dynWeek contains((key as String))))
}) )

---
{
    "totPills": payload.report[0].totPills,
    "report":pl1 ++ pl2 /* Not producing expected results */
}

I have a few questions:

  1. How to achieve the Expected Output dynamically?
  2. Is there any performance impact in this approach?

Thanks, Rick

I separated the mapping of report into two sets of key-values, whose keys are in dynWeek, and those who aren't.

%dw 2.0
output application/json

var dynWeek = ['202201','202202','202203']

fun getInList(obj: Object, list: Array, in: Boolean)= obj filterObject ( 
    do {
        var isIn = list contains ($$ as String)
        ---
        if (in) isIn else !isIn
    }
)
---
{
    "totPills": payload.report[0].totPills,
    "report": payload.report 
                map ({
                        (getInList($ - "totPills", dynWeek, false)), // fields not in dynWeek
                        qty: getInList($, dynWeek, true)  // fields in dynWeek
                    })
}

Output:

{
  "totPills": 100,
  "report": [
    {
      "food": "chm",
      "drink": "kgf",
      "qty": {
        "202201": 1,
        "202202": null,
        "202203": 0.39
      }
    },
    {
      "food": "mkm",
      "drink": "wfl",
      "qty": {
        "202201": 0.0,
        "202202": 1.36,
        "202203": 0
      }
    }
  ]
}

This should work:

%dw 2.0
output application/json
---
{
    "totPills": payload.report[0].totPills,
    "report": payload.report map {
        food: $.food,
        drink: $.drink,
        qty: ($ - "food" - "drink" - "totPills")
    }
}

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