Re: Validate JSON Has Map Data

Adam
Level 2

Validate JSON Hash Map Data

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

Re: Validate JSON Has Map Data

Can y'highlight it please? 🙂

 

syntax_vtl.png

Adam
Level 2

Re: Validate JSON Has Map Data

#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

Re: Validate JSON Has Map Data

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

Re: Validate JSON Has Map Data

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

Re: Validate JSON Has Map Data

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

Re: Validate JSON Has Map Data

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

Re: Validate JSON Has Map Data


#set( $CID = $lead.ClinicID )

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

Adam
Level 2

Re: Validate JSON Has Map Data

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

Re: Validate JSON Has Map Data

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.