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

Add a default fallback value to paths #531

Merged
merged 1 commit into from
Oct 5, 2023
Merged

Add a default fallback value to paths #531

merged 1 commit into from
Oct 5, 2023

Conversation

satabin
Copy link
Member

@satabin satabin commented Oct 5, 2023

In some query languages and for some formats, when a path is not found, it is fine to output nothing. For instance for XML, an empty sequence of nodes is ok.

However in some cases, a default value has to be output. This is the case in the JSON query language. When the query builds an object whose value is a sub-query, then the value cannot be empty, even if the sub-query finds no element. Moreover, the query must return exactly one result. If it return zero or more than one element, then the generated JSON data is invalid.

But in the JSON query lanuage, generating no data is still valid, and happens when the path is iterated over, for instance.

This change allows for the query to parameterize for each path whether it should return a (unique) default value, or not. If not, then the previous behavior is kept, otherwise in case of no-match, then the default value is output.

In some query languages and for some formats, when a path is not found,
it is fine to output nothing. For instance for XML, an empty sequence of
nodes is ok.

However in some cases, a default value has to be output. This is the
case in the JSON query language. When the query builds an object whose
value is a sub-query, then the value cannot be empty, even if the
sub-query finds no element. Moreover, the query must return exactly one
result. If it return zero or more than one element, then the generated
JSON data is invalid.

But in the JSON query lanuage, generating no data is still valid, and
happens when the path is iterated over, for instance.

This change allows for the query to parameterize for each path whether
it should return a (unique) default value, or not. If not, then the
previous behavior is kept, otherwise in case of no-match, then the
default value is output.
@satabin satabin added enhancement New feature or request json finite state labels Oct 5, 2023
@satabin satabin added this to the 1.9.0 milestone Oct 5, 2023
@satabin satabin requested a review from a team as a code owner October 5, 2023 16:00
Copy link
Collaborator

@ybasket ybasket left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@satabin satabin merged commit 628dd71 into main Oct 5, 2023
29 checks passed
@satabin satabin deleted the json/jq/null branch October 5, 2023 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants