Forum Discussion

Charles's avatar
Charles
New Contributor II
4 years ago
Solved

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

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'])
  • @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.

10 Replies

  • viktor_n's avatar
    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.

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

    There is the output.

    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's avatar
    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.

    • Charles's avatar
      Charles
      New Contributor II

      @del, really appreciate the working example, thank you! Like you mention, I don’t think VTL outweighs putting in a mapper snap, but really glad to learn about VTL as a tool for potential future solutions. I guess I’ll have to read up on that a bit. Thank you for the start!

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Your first expression seems ok, but can it happen that the first object is also optional? If that’s so, you should use the .get() method at both levels:

    $.get('wd:Project_Data').get('wd:End_Date')

    Note that the .get() method allows for a default value:

    $.get('wd:Project_Data','DEFAULT VALUE')

    • Charles's avatar
      Charles
      New Contributor II

      Thanks for the reply @bojanvelevski.

      I think the issue may be that expressions are not allowed in the JSON Generator snap? The solution to my problem is easy enough, I’ll just put a mapper snap before my JSON Generator snap and use the “null-safe access” option to fill in the blanks where needed, but I am surprised this isn’t an option in the JSON Generator snap, and I’m also not sure if any expressions are allowed.

      See the very basic chain-JSON Generator pipeline that demonstrates the problem. I can’t reference the input object with an $.get(“num”) expression.

      JSON Generator Expression Test_2021_12_06.slp (4.7 KB)