Validate JSON Hash Map Data | Community
Skip to main content
Level 2
May 5, 2020
Question

Validate JSON Hash Map Data

  • May 5, 2020
  • 1 reply
  • 5367 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

SanfordWhiteman
Level 10
May 5, 2020

Can y'highlight it please? 🙂

 

Adam-19Author
Level 2
May 5, 2020
#set($CID = ${lead.ID}) #set($NME = "$clinics.Map.get($CID).NME") #if( $CID.isEmpty() || $NME.isEmpty() ) Physiotherapy #else $NME #end
SanfordWhiteman
Level 10
May 5, 2020

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".