2

I have a session variable called varIP1 which has a value like below: (It's mime type is application/java)

{abc={FedId=abc, Id=01FcLA, type=User, Profile={Id=02EmQA, type=Profile, Name=Analyst}}}

I am interested in extracting the first Id (01FcLA) from above using a function as given below:

%dw 1.0
%output application/json
%var myLib = readUrl("classpath://dwlib/my-global-functions.wev", "application/dw")
---
{

    "Id": (myLib.idLookup(sessionVars.varIP1 ,$.Id,"Id") default null)
}

The global function that I'm using are:

%function validateLookup(lookupArray, key) 'true' when (lookupArray != null and IsNull(key) == 'false' and lookupArray[key] != null) otherwise 'false'
%function idLookup(lookupArray,key,value) (lookupArray[key][0][value] default '') as :string when (validateLookup(lookupArray,key) == 'true')  otherwise null

With the above code, "Id" is coming as null. Any modifications needed above?

Thanks.

Triumph Spitfire
  • 663
  • 15
  • 38
  • What is `$.Id` supposed to be? That should not work outside a closure. Unrelated to the question, why the functions use string literals 'true' and 'false' instead of boolean values true and false? – aled Feb 20 '20 at 13:45
  • Thanks for your reply. Could you please correct the code for me? I need to retrieve the Id value from the sessionVars array. I can change the functions to boolean values as well. – Triumph Spitfire Feb 20 '20 at 13:49

1 Answers1

0

This would be the simplest approach if you know that your sessionVar will have the same structure:

%dw 1.0
%output application/json
---
{
    Id: sessionVars.varIP1..Id
}

Updated, since abc can't be hardcoded.

There's no reason to put default null. If it can't find the Id, it will be null. If you want to default to "", that would make a little more sense.

utechtzs
  • 1,013
  • 5
  • 12