Re: Validate JSON Has Map Data

Adam
Level 2

I am using a JSON hash map and would like to validate that I am getting data, and if not that I provide a "Default value. I cannot get it to check properly if my hash has a value or not. For some reason the $NME is already returning true.  I am trying to account for cases where I don't have a match or the key NME doesn't exist. What am I missing (CID Is Empty works, NME isEmpty doesn't work/always returns true)?

 

#set($CID = ${lead.ID})
#set($NME = "$clinics.Map.get($CID).NME")

#if( $CID.isEmpty() || $NME.isEmpty() )
Physiotherapy
#else
$NME
#end

30 REPLIES 30
SanfordWhiteman
Level 10 - Community Moderator

Can y'highlight it please? 🙂

 

syntax_vtl.png

Adam
Level 2
#set($CID = ${lead.ID})
#set($NME = "$clinics.Map.get($CID).NME")

#if( $CID.isEmpty() || $NME.isEmpty() )
Physiotherapy
#else
$NME
#end
SanfordWhiteman
Level 10 - Community Moderator

First, let's clean up the syntax a bit, the curlies and the quotes aren't right for #set directives.

 

#set( $CID = $lead.ID )
#set( $NME = $clinics.Map.get($CID).NME )

#if( $CID.isEmpty() || $NME.isEmpty() )
Physiotherapy
#else
$NME
#end

 

Now, the principal problem seems to be that you're expecting $NME to be empty â€” as in, an empty String — if the chain of property dereferences has an error.

 

But that isn't the case. It's more likely to be null

 

Take this $lead:

{
  ID = 9999
}

 

And this $clinics:

{
  Map = {
    1234 = {
      NME = Sandy
    }
  }
}

 

If you do this:

#set( $NME = $clinics.Map.get($CID).NME )

 

Then $NME will be null â€” and you can't use that to match isEmpty() (you'll have another swallowed null).

 

What you more likely want is to use containsKey, rather than letting nulls be swallowed up by the parser.

 

For example:

#set( $iDInMap = $clinics.Map.containsKey($CID) )

 

This will set $iDInMap to a clear Boolean true or false depending on whether the ID is found. You could do the same to see if it has a true key called "NME".

Adam
Level 2

Hi,

Thank you for the detailed reply, it makes perfect sense now! However when I try to do it, if i adjust to the syntax you provided I am not getting an output in my test email, it just prints "$CID", etc. When I add the Quote and {} I get CID and NME to print, but I cannot figure out what syntax makes the IDInMap work.

 

#set( $CID = $lead.ID )
#set( $NME = $clinics.Map.get($CID).NME )
#set( $iDInMap = $clinics.Map.containsKey($CID) )
$CID
$NME
<br/>
$iDInMap
<br/>

 

this syntax (aside from iDInMap} does print the proper values

 

#set( $CID = ${lead.ID} )
#set( $NME = "$clinics.Map.get($CID).NME" )
#set( $iDInMap = $clinics.Map.containsKey($CID) )
$CID
$NME
<br/>
$iDInMap
<br/>

 

SanfordWhiteman
Level 10 - Community Moderator

More like so:

#set( $NME_DEFAULT = "Physiotherapy" )
#set( $CID = $lead.ID )
#if( !$CID.isEmpty() && $clinics.Map.containsKey($CID) && $clinics.Map[$CID].containsKey("NME") )
#set( $NME = $clinics.Map[$CID]["NME"] )
${NME}
#else
${NME_DEFAULT}
#end

 

get() is always superfluous btw.

Adam
Level 2

I don't know why this isn't working for me, as everything below makes sense. Currently it's always returning false.

 

I broke out the IF statements to see why, and the "IS Empty" is always returning true, and the other two are always returning false.

I am testing 3 scenarios to make sure it all works, an ID that exists, an ID that doesn't exist, and an empty ID.

 

#set( $NME_DEFAULT = "Physiotherapy" )
#set( $CID = $lead.ClinicID )
#if( !$CID.isEmpty() && $clinics.Map.containsKey($CID) && $clinics.Map[$CID].containsKey("NME") )
#set( $NME = $clinics.Map[$CID]["NME"] )
${NME}
#else
${NME_DEFAULT}
#end
$CID
#if ( !$CID.isEmpty() )
	The clinic has a value
#end

#if ( $clinics.Map.containsKey($CID) )
	The clinic exists in the hash
#end

#if ( $clinics.Map[$CID].containsKey("NME") )
	The has has NME
#end

 

 

SanfordWhiteman
Level 10 - Community Moderator

#set( $CID = $lead.ClinicID )

In your initial code, you had the Lead field as $lead.ID, not $lead.ClinicID.

Adam
Level 2

ClinicID is the correct name, and it has data as expected. The issue is that the IF statements are not evalutating properly.

SanfordWhiteman
Level 10 - Community Moderator

ClinicID is the correct name, and it has data as expected. 


OK, kind of confusing when the property name changes though...

 


The issue is that the IF statements are not evalutating properly.

With this $lead:

{ClinicID=9999}

 

And this $clinics

{Map=
  {9999=
    {NME=Sandy}
  }
}

 

Your code outputs:

Sandy
9999
        The clinic has a value

        The clinic exists in the hash

        The has has NME

 

With the same $lead and and this $clinics:

{Map=
  {1234=
    {NME=Sandy}
  }
}

 

Your code outputs:

Physiotherapy
9999
        The clinic has a value

 

With the same $lead and this $clinics:

{Map=
  {9999=
    {ABC=Joe}
  }
}

 

Your code outputs:

Physiotherapy
9999
        The clinic has a value

        The clinic exists in the hash

 

The output is expected in each case.

Adam
Level 2

So the first problem is if Clinic ID has no value, it's printing the variable in the email.

The second issue is if the value of clinicID doesn't exist, it's still printing the variable in my email (like the below)

$clinics.Map[$CID]["NME"]

 

So that is why I am saying the IF statements are not evaluating properly. I am testing with 3 emails, one covering each use case. Proper ID, Missing ID, and Wrong ID

SanfordWhiteman
Level 10 - Community Moderator

The output is exactly what you'd expect from the code you have. It's evaluating properly.

 

$reference.isEmpty() is null if $reference is null, because you can't check emptiness of non-String/Array values.

 

!$reference.isEmpty() is true if $reference is null, because the null is coerced to a Boolean when you use !.

 

 

Adam
Level 2

Ok that makes sense, so how do I check if it's null? Putting null I get an error.

SanfordWhiteman
Level 10 - Community Moderator

if $reference is null

$display.alt($reference,"").isEmpty() is true 

 

if $reference is ""

!$display.alt($reference,"").isEmpty() is false

 

if $reference is "Sandy"

!$display.alt($reference,"").isEmpty() is true 

 

Adam
Level 2

I'm sorry, I am really not getting how to check if the value is null, that's the part I have been stuck on for this entire post.

 

Doing this

if $reference is null

$display.alt($reference,"").isEmpty() is true 

 

Is always returning true for me, regardless of the value of "Reference",

#set( $NME = "Physiotherapy" )
#set( $CID = $lead.ClinicID )
#if( ${CID.isEmpty()} == 'false' && ${clinics.Map.containsKey($CID)} == 'true' && ${clinics.Map[$CID].containsKey("NME")} == 'true' )
   #set( $NME = $clinics.Map[$CID]["NME"] )
   <br>
   All statements validate to TRUE STRING<br>
#end
$CID

#if ( $display.alt($CID,"").isEmpty() )
	The clinic has a value
#end

 

SanfordWhiteman
Level 10 - Community Moderator

$display.alt() returns its 1st argument if the 1st argument is anything other than null.  Otherwise, it returns the 2nd argument. 

 

The method is designed to be a null coalescing function, like COALESCE in SQL.

 

It's not possible for $display.alt($reference,"").isEmpty() to return true if $reference is a non-empty String.

Adam
Level 2

So this still isn't working, it must mean that when that data doesn't exist it's something other then "null", how can we figure out what is going on?

 

It always returns true for me, it doesn't matter if there is a clinic ID or not, and the contains never prints even if it does exist. 

Adam
Level 2

If $lead.ClinicID ) does not have a value in marketo, I want to print an alternative instead, I have tried to check if it's null, true/ false, or an empty string, but none of them are working properly.

 

How do I check if CID has a value or not?

 

#set( $CID = ${lead.ClinicID} )
SanfordWhiteman
Level 10 - Community Moderator
#if( $lead.clinicId.isEmpty() )
clinicId is explicitly empty
#elseif( $display.alt($lead.clinicId,"").isEmpty() )
clinicId is null
#else
clinicId has a non-empty String value ${lead.clinicId}
#end
Adam
Level 2

No change:

 

  • $lead.clinicId has a value of 123456 (Based on data in field and what i print on the page)

    • Result clincID is null
  • $lead.clinicId has no value in the marketo field (Based on data in field and what i print on the page)
    • Result clincID is null

The exact Code I did, CID is printing either the numbers (123456) or nothing as expected.

#set( $CID = $convert.toString( ${lead.ClinicID} ) )
#if( ${CID.isEmpty()} == 'false' && ${clinics.Map.containsKey($CID)} == 'true' && ${clinics.Map[$CID].containsKey("NME")} == 'true' )
   #set( $NME = $clinics.Map[$CID]["NME"] )
   <br>
   All statements validate to TRUE STRING<br>
#end
$CID

Below is the content<br/>
#if( $lead.ClinicID.isEmpty() )
clinicId is explicitly empty
#elseif( $display.alt($lead.ClinicID,"").isEmpty() )
clinicId is null
#else
clinicId has a non-empty String value ${lead.ClinicID}
#end

 

SanfordWhiteman
Level 10 - Community Moderator

Can only imagine you're spelling the property name wrong.