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

How the Object Key Filter work #370

Open
ext-singlad opened this issue Nov 20, 2024 · 9 comments
Open

How the Object Key Filter work #370

ext-singlad opened this issue Nov 20, 2024 · 9 comments
Labels
support Users asking how to solve a specific issue

Comments

@ext-singlad
Copy link

Hi,

We have been using this library and incredibly happy with the results, saves our lot of time in Json transformations.

We have a scenario where it is required not to omit the keys where value is null or empty string.
I found that this can be achieved as explained here -
https://github.com/schibsted/jslt/blob/master/docs/api.md#object-key-filter

Now in the given syntax if I replace the filter with ". == null or . == {} or . == []" I don't get the desired result, I am getting emply json in the output, when I pass true, I get the expected results. Can you please help me with the correct way how to pass the `. == null or . == {} or . == []' in the filter?

Expression e = new Parser(reader)
  .withObjectFilter(". == null or . == {} or . == []")
  .compile();

JSLT:

{
        "commodity": {
            "en": .COMMODITY_NAME_EN,
            "zh": .COMMODITY_NAME_ZH
        }
    }

Input Json:

{
"COMMODITY_NAME_EN": "ABC",
}

Expected Output Json:

{
        "commodity": {
            "en": "ABC",
            "zh": null
        }
    }
@larsga larsga added the support Users asking how to solve a specific issue label Nov 20, 2024
@larsga
Copy link
Collaborator

larsga commented Nov 20, 2024

By default JSLT omits some values from objects, such as null. By setting the filter you can provide a new rule for what should be omitted. Basically, JSLT omits a value when the filter returns false.

So if the filter is true then no values are omitted, since the filter always evaluates to true. You're basically turning filtering off. That sounds like what you want?

@ext-singlad
Copy link
Author

With true it is working fine.
how about if I have to discard values fields only if the value is null(. == null) and retain all other fields with empty json({}) and array([]).

@larsga
Copy link
Collaborator

larsga commented Nov 20, 2024

You want the filter to return true when the value should be omitted, right? So you want . != null. In other words: keep everything except null.

@ext-singlad
Copy link
Author

yes that's correct.

@larsga
Copy link
Collaborator

larsga commented Nov 20, 2024

That's good. Can we close this issue now?

@ext-singlad
Copy link
Author

Sorry bit confused.
If I pass in the filter ". == null"
will it only consider null and omit others or vice versa.

@larsga
Copy link
Collaborator

larsga commented Nov 20, 2024

The filter says what to keep. So if you say . == null it means you only want to keep null.

@ext-singlad
Copy link
Author

But it is not working. if I am passing . == null getting empty json {} in the output for the shared example.

@larsga
Copy link
Collaborator

larsga commented Nov 21, 2024

This is my previous comment. It explains why you get an empty JSON:

So if you say . == null it means you only want to keep null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Users asking how to solve a specific issue
Projects
None yet
Development

No branches or pull requests

2 participants