3

I need to check the existence of a particular keyword in a JSON object and based on that, create a string. My sample dataweave 2.0 code is given below:

%dw 2.0
output application/json
var arr = {
    "ID": "100",
    "ProdId": "Prod",
    "ProdName": "ABC"
           }
---
if (arr pluck $$ joinBy ';' contains "ProdId") == true
    then (String: "Product") else
if (arr pluck $$ joinBy ';' contains "EmpId") == true
    then (String: "Employee") else null

Based on the value of the variable, I need to have different String values. What change is needed above to work?

Triumph Spitfire
  • 663
  • 15
  • 38

2 Answers2

4

I'm thinking about using the "keysOf" function (previously Object::keySet) and something like this:

%dw 2.0
output application/json
var arr = {
    "ID": "100",
    "ProdId": "Prod",
    "ProdName": "ABC"
           }
---
if (keysOf(arr) contains("ProdId" as Key)) "Product"
else if (keysOf(arr) contains("EmpId" as Key)) "Employee"
else "Not Recognized"

Note that "keysOf" returns an array of Key objects, not Strings.

EDIT:

A more dynamic approach can be this one:

%dw 2.0
output application/json
import firstWith from dw::core::Arrays
var arr = {
    "ID": "100",
    "ProdId": "Prod",
    "ProdName": "ABC"
           }
var myTypes = {
    ProdId: "Product",
    EmpId: "Employee"
}
var typeId = (keysOf(myTypes) firstWith (keysOf(arr) contains $)) as String
---
myTypes[typeId]
Jorge Garcia
  • 1,313
  • 8
  • 14
4

You can take advantage of the field existence operator ?:

%dw 2.0
output application/json
var o = {
    "ID": "100",
    "ProdId": "Prod",
    "ProdName": "ABC"
}
---
if (o.ProdId?) (String: "Product") else 
    if (o.EmpId?) (String: "Employee") else null

You can solve your problem with pattern-matching by using the match operator as well:

%dw 2.0
output application/json
var o = {
    "ID": "100",
    "ProdId": "Prod",
    "ProdName": "ABC"
}
---
"ProdId" match {
    case "ProdId" -> "String": "Product"
    case "EmpId"  -> "String": "Employee"
    else -> null
}

Here's the documentation:

You also need to be aware that String is a type in DW, you must enclose it in quotes (" or ') to use as a field name.

George
  • 2,758
  • 12
  • 16
  • Should the second example match a String? or it should be variable `o` instead? – aled Jun 11 '20 at 17:46
  • Could be a `String`, it could be a variable, it can be a `Key`, It can be all of the above--hence the power of pattern-matching Aled! Of course if you would like to combine all of the above the `case`s should be adjusted to cover for all different inputs. – George Jun 11 '20 at 17:48
  • I mean that the variable is not used in the script. It seems to have been copied from the previous example. Was it left there unintentionally? – aled Jun 11 '20 at 21:13
  • Oh I see what you mean. Yes I did skip a couple of steps (i.e. how to access the fields and then apply them to match) because the fields were hard-coded in the first place. – George Jun 12 '20 at 11:11
  • Just to note, for anyone searching in the Mule documentation, `?` is a DataWeave selector, called the "key present" selector. Current documentation is published here: https://docs.mulesoft.com/dataweave/2.4/dataweave-selectors – Stephen Hartley Apr 08 '22 at 13:08