Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty values returned by JSONPath #2043

Open
ghTravis opened this issue Apr 17, 2024 · 1 comment
Open

Allow empty values returned by JSONPath #2043

ghTravis opened this issue Apr 17, 2024 · 1 comment
Milestone

Comments

@ghTravis
Copy link

ghTravis commented Apr 17, 2024

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

@hairyhenderson
Copy link
Owner

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 hairyhenderson added this to the future milestone May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants