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
Adam
Level 2

If that was the case it wouldn't be printing the value on the page.

SanfordWhiteman
Level 10 - Community Moderator

A direct {{lead.token}} name isn't the same as the property name in VTL.

Adam
Level 2

I"m sorry I don't understand what you mean on this? I shared the code I have do you see what I have wrong?

SanfordWhiteman
Level 10 - Community Moderator

I'm saying that being able to output {{lead.Field Name}} directly in an email doesn't mean the field is accessible as $lead["Field Name"] in Velocity.

 

You should assume the token name and the Velocity property name are different (occasionally they're the same, but consider that a coincidence). You only know the Velocity name when you're in the Velocity editor.

Adam
Level 2

Am I not in the velocity editor with this?

 

Screen Shot 2020-06-08 at 9.32.13 PM.png

SanfordWhiteman
Level 10 - Community Moderator

Yes, that's the Velocity Script Editor, of course.

 

But elsewhere, you've referred to {{lead.token}} syntax. That isn't Velocity, and the ability to output a {{lead.token}} in the body of an email doesn't predict anything about Velocity code and/or naming conventions.

 

In any case, the code provided to check the null-ness or empty-ness of a String property is time-tested and correct. As for what else is going wrong on your side... can't say without access to your instance.

Adam
Level 2

I don't understand what is going on, since switching from the SCRIPT token to store my hash, to storing it in a TEXT token (because we can no longer update SCRIPT with API) none of my IF statements that previously worked are functional (They always return FALSE), and I cannot find any way to get them working. Everything else is working as expected (Printing date, loops etc).

Because i can print data and retrive it as before I can't see my data structure being any different.

Also my Lead Tokens for direct values the IF statements work properly, but not checking values against my text token.

 

Where clinic's is the hash map, below is always FALSE( checking if it exists). So what is different about IF statements and Text Tokens?

#if($clinics.Map.get($item) .

 

Below is a lead field, it works:

#if ($leadData.Map.get($item).apptAllowed == 1)

Adam
Level 2

It feels like since the switch to a TEXT token, like marketo isn't full recognizing this as an OBJECT and that, as soon as I try to assign a key into a variable or do an IF statement, it doesn't save it.

SanfordWhiteman
Level 10 - Community Moderator

What is it recognizing it as, if not a LinkedHashMap?

Adam
Level 2

I have no idea, and no way to tell. I just know all the IF statements and the code i used to even save a variable from the TEXT token doesn't work, and it works just fine if it's a SCRIPT token. That's the only thing that's changed.

 

If i try to do a simple #set variable  and then just print it, I get the variable printed on the page no matter what i do. WIth the result in a script token that doesn't happen.

 

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

 

This will just print $CID on the page, if I use a SCRIPT token to store Clinics instead of Text, it works as expected.

If i just print $clinics.Map.get($CID).NME it works as expected. We tried a ton of variations on stuff in this thread above, and i think somehow it's coming back to it being a TEXT tokens vs a SCRIPT, but I have no idea how to handle it.