cancel
Showing results for 
Search instead for 
Did you mean: 

Null Safe access option for object value reference in JSON Generator snap?

Charles
New Contributor II

Hi,

I’m trying to reference a value from the input object in the JSON Generator snap. The property is optional in the upstream snaps. If I straight reference it, I’ll get an “Invalid JSON-Path field not found” error.

Is there a similar way to the use “null-safe access” option like in the mapper snap in the JSON Generator snap?
Is there a way to use the $.get() method in the JSON Generator snap? I keep getting syntax errors.

Below is my property reference:
$['wd:Project_Data']['wd:End_Date']

This fails:

$['wd:Project_Data'].get("wd:End_Date")
$['wd:Project_Data'].get(['wd:End_Date'])
$.get(['wd:Project_Data']['wd:End_Date'])
1 ACCEPTED SOLUTION

del
Contributor III

@Charles,
Like Patrick, I was also thinking Velocity (VTL) as a solution, but as I tried to solution your original post, the Workday namespace convention in the property names adds some complexity. I don’t know if Velocity beats the null-safe mapper for your particular use case, but it can be good info for your tool belt in the future.

The kind of ugly solution I came up with (in a one-liner) is:

[
    {
       "output" : #set ($pd = $['wd:Project_Data']) #set ($ed = $pd['wd:End_Date']) #if (${ed}) ${ed} #else null #end
    }
]

I’m not well versed in VTL, so there’s likely something a little cleaner than the above.

View solution in original post

10 REPLIES 10

Charles
New Contributor II

Hi @viktor_n,

I’m now pretty certain that expressions don’t work in the JSON Generator snap at all (regardless of square brackets or colons). Posting to see if anyone can confirm, but I think the snap only takes pure JSON and a direct variable reference. It’s just a shame that it’s not null safe, so it requires another mapper snap before it.

The basic pipeline below highlights the issue, that no expressions, like “$.get()” are allowed in the snap.

JSON Generator Expression Test_2021_12_06.slp (4.7 KB)

Thanks,
Charles

@Charles Please note the following documentation at the top of the default template in the JSON Generator:

Enter your JSON-encoded data in this space. Note that this text is
treated as an Apache Velocity template, so you can substitute values
from input documents or the pipeline parameters. See the following
URL for more information about Velocity:
https://velocity.apache.org/engine/devel/user-guide.html

This means you can use Velocity syntax, not SnapLogic EL syntax, in your templates, like this:

[
    {
        "result" :
#if($msg)
        ${msg}
#else
        ${fallback}
#end
    }
]

Here’s a pipeline to demonstrate this:
Velocity Null Check_2021_12_07.slp (4.8 KB)

You can do the same on a single line like this:

        "result" : #if($msg) $msg #else $fallback #end

Charles
New Contributor II

Got it. That makes sense. Thank you for the explanation and syntax example!

viktor_n
Contributor II

Hi @Charles,

just try to map the field without any expression functions.

This is what I put in mapper before JSON Generator.
image

And here is what I am trying to map in JSON Generator.
image

There is the output.
image

But if you want to get null instead of empty string that would be different.
You can try to do it with apache velocity or maybe after the generator set one Mapper and replace all empty strings with null.

Regards,
Viktor

del
Contributor III

@Charles,
Like Patrick, I was also thinking Velocity (VTL) as a solution, but as I tried to solution your original post, the Workday namespace convention in the property names adds some complexity. I don’t know if Velocity beats the null-safe mapper for your particular use case, but it can be good info for your tool belt in the future.

The kind of ugly solution I came up with (in a one-liner) is:

[
    {
       "output" : #set ($pd = $['wd:Project_Data']) #set ($ed = $pd['wd:End_Date']) #if (${ed}) ${ed} #else null #end
    }
]

I’m not well versed in VTL, so there’s likely something a little cleaner than the above.