1

I am running %dataweave 1.0 . I need to create a function which should replace some code values according to a certain payload which is received from an API call in my Mule flow. The sample API output is given below: This is stored in a session variable currently.

{
    "CodeMaster": {
        "PrimaryCodes": {
            "PrimarySpecCodes": {
                "ABC": {
                    "code": "Alpha Bravo Charlie",
                    "target": "SALES",
                    "Field": "PrimarySpecCodes"
                },
                "TUV": {
                    "code": "Tango Umbrella Victor",
                    "targetSystemCode": "SALES",
                    "targetCodeFieldName": "PrimarySpecCodes"
                },
                "XYZ": {
                    "code": "X-Ray Yankee Zulu",
                    "targetSystemCode": "SALES",
                    "targetCodeFieldName": "PrimarySpecCodes"
                }
            }
        }
    }
}

As shown above, I need to create a function which will replace the codes (like ABC, TUV, XYZ) in my main payload with the values "Alpha Bravo Charlie", "Tango Umbrella Victor" and "X-Ray Yankee Zulu" respectively. In the main payload, I have the data to be replaced like below:

"PY123":
  {
      "Country": "GB",
      "Status": "ACTIVE",
      "Flag": null,
      "SpecCodes": [
        {
          "PrimarySpecCodes": "ABC"
        },
        {
          "PrimarySpecCodes": "TUV"
        },
        {
          "PrimarySpecCodes": "XYZ"
        }
      ]
    }

How can I create a function to replace the code values. If there is a better solution to replace codes, please suggest. Thanks in advance.

Triumph Spitfire
  • 663
  • 15
  • 38

1 Answers1

3
%dw 1.0
%output application/json

%function buildLookup(codes)
  codes mapObject {($$): $.code}

%var codeLookup = buildLookup(sessionVars.code.CodeMaster.PrimaryCodes.PrimarySpecCodes)

%var verboseCodes = payload.PY123.SpecCodes map (code) ->
  code mapObject {($$): codeLookup[$]}
---
{
  "PY123" : {
    "Country"   : "GB",
    "Status"    : "ACTIVE",
    "Flag"      : null,
    "SpecCodes" : verboseCodes
  }
}
jerney
  • 2,187
  • 1
  • 19
  • 31
  • Thanks for the answer. I am on dataweave 1.0 and get some errors while defining the function. Could you advise? – Triumph Spitfire Aug 27 '19 at 14:58
  • I prototyped it in 2.0, please try w/ the edited version – jerney Aug 27 '19 at 15:42
  • Thank you. I'm getting the following error though: Exception while executing: code mapObject {($$): codeLookup[$]} ^ Type mismatch for 'Dynamic Selector' operator found :object, :array required :object, :name or – Triumph Spitfire Aug 27 '19 at 16:55
  • Alternatively, let's say I have the codeLookup string as below: { "ABC": "Alpha Bravo Charlie", "TUV": "Tango Umbrella Victor", "XYZ": "X-Ray Yankee Zulu" } Is there a way to replace the codes in a string using the values above: **From** "PrimarySpecCodes": "ABC;TUV;XYZ" **to** "PrimarySpecCodes": "Alpha Bravo Charlie;Tango Umbrella Victor;X-Ray Yankee Zulu" – Triumph Spitfire Aug 27 '19 at 17:58
  • It's not easy to manipulate strings directly for that kind of stuff. You'll want to get it into an array first using `splitBy`. Then use `map` to make your change, then `joinBy` to get back to the string – jerney Aug 27 '19 at 18:42
  • Not sure about your error. Please check the inputs to `buildLookup` and make sure they make sense. – jerney Aug 27 '19 at 18:44
  • Sorry to bother you again. I haven't got it working still. The function works fine and gives the output like this: { "ABC": "Alpha Bravo Charlie", "TUV": "Tango Umbrella Victor", "XYZ": "X-Ray Yankee Zulu" } Now how can I use the lookup values from the function in this sort of a string: {"PrimarySpecCodes": ["ABC", "TUV", "XYZ"]} (It's slightly different now) – Triumph Spitfire Aug 28 '19 at 18:48
  • If it's different now you should either edit your question or open a new one. – jerney Aug 29 '19 at 19:11
  • 1
    @TriumphSpitfire, look into the `map` function and within the lambda body pass the value to codeLookup[$]. E.g. `payload.PrimaySpecCodes map codeLookup[$]` – jerney Aug 29 '19 at 20:34