gomplate icon indicating copy to clipboard operation
gomplate copied to clipboard

Allow empty values returned by JSONPath

Open ghTravis opened this issue 10 months ago • 1 comments

Hi,

I am trying to use coll.JSONPath to evaluate a particular key path structure but if for whatever reason the path does not exist, the template generation errors with error:

ERR error="renderTemplate: failed to render template <arg>: template: <arg>:1:93: executing \"<arg>\" at <coll.JSONPath>: error calling JSONPath: executing JSONPath failed: baz is not found"

You can test this by running the following gomplate command:

gomplate -i '{{ $my_list := dict "foo" (dict "bar" (dict "val1" "10" "val2" "test")) }} {{ $my_list | coll.JSONPath "$.foo.baz" }}'

It would be great if JSONPath could support returning empty values when the path is not found, perhaps based on the missingKey setting. Some support in favor of adding this capability to coll.JSONPath can be seen in this comparison doc of the various different supporting libraries for JSONPath that have support for "empty" return values.

It could be as simple as changing this line of code to honor the missingKey value https://github.com/hairyhenderson/gomplate/blob/main/coll/jsonpath.go#L53

=== Gomplate Version: v4.0.0-pre-2

ghTravis avatar Apr 17 '24 17:04 ghTravis

This could perhaps be supported by way of the flag added in #1948/#1949. It'll take a bit of wiring but I think it's feasible.

@ghTravis are you willing to try implementing this? Otherwise it's likely to take a while for me to get around to this.

hairyhenderson avatar May 12 '24 15:05 hairyhenderson