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

Support parameters and/or parent/root level objects #279

Open
jingkunhub opened this issue Jun 16, 2023 · 6 comments
Open

Support parameters and/or parent/root level objects #279

jingkunhub opened this issue Jun 16, 2023 · 6 comments

Comments

@jingkunhub
Copy link

I’m expecting a feature of supporting parameters assigned with internal or external json fragments in the same way as in XSLT. One of the use cases is to use such parameterized objects inside #loop. Or it’ll service the same purpose if providing a mechanism to access root/parent level fragments.

I am not able to find similar functionality in the current code base.

@Courela
Copy link
Contributor

Courela commented Jun 16, 2023

There's an alias called 'root' for accessing properties from root element/input, if that's what you want. That feature is not documented though...

PS: #valueof path was always an absolute path, $ was a reference to input's root, I don't recall if it still behaves that way...

@jingkunhub
Copy link
Author

Great! I will try what you suggested and reply with my results ASAP.

@jingkunhub
Copy link
Author

Hi Courela,

Just confirmed that #valueof path always the absolute one. This is exactly what I need for now.

Also, I tried $ reference to the input root, with $, $. #valueof($). All of them gave me the null output. What's the exact usage?

Thanks,
Jingkun

@Courela
Copy link
Contributor

Courela commented Jun 17, 2023

Dollar sign refers to the root/start of the element being processed, as described in JsonPath. For #valueof function, it's always the whole input, but if you're in a loop, every other function will reference the element being processed, if no alias is specified. If you want to refer to a property/element outside the loop, you can use alias, being 'root' a special alias that references whole input.

Usually, the transformation returns null if there's an error, one can change that creating a transformer using EvaluationMode.Strict

new JsonTransformer(new JUSTContext { EvaluationMode = EvaluationMode.Strict })

@jingkunhub
Copy link
Author

jingkunhub commented Sep 25, 2023

Hi Courela,

Than you very much for your suggestion. using EvaluationMode.Strict just help me identified the root cause of an issue - only an actual value allowed in the following condition where I'm trying to compare a value from #valueof or #currentvalueatpath,

Input:
{ "open": "open", "close": "close", "menu": { "popup": { "menuitem": [ { "value": "Open", "onclick": "OpenDoc()" }, { "value": "Close", "onclick": "CloseDoc()" } ], "submenuitem": "CloseSession()" } } }

Transformer:
{ "result": { "Open": "#valueof($.menu.popup.menuitem[?(@.value == #valueof($.open)].onclick)", "Close": "#valueof($.menu.popup.menuitem[?(@.value == #valueof($.close))].onclick)" } }
The error is 'Error while calling function : #valueof($.menu.popup.menuitem[?(@.value == #valueof($.open)].onclick) - Unexpected character while parsing path query: #

I used the first example of valueof to illustrate my use case of feeding #valueof as the 2nd comparator.

How to handle dynamic values in the above sample?

Thanks,
Jingkun

@Courela
Copy link
Contributor

Courela commented Sep 25, 2023

You have to use #concat/#xconcat to make a dynamyc expression/path to #valueof

{ "result": { "Open": "#valueof(#xconcat($.menu.popup.menuitem[?/(@.value == ',#valueof($.open),'/)].onclick))", "Close": "#valueof(#xconcat($.menu.popup.menuitem[?/(@.value == ',#valueof($.close),'/)].onclick))" } }

Note that you have to escape parenthesis inside the dynamic expression, or they will be considered as a function open/close instead of a character of a string

.menuitem[?/(@.value

Two notes:

  • since you are comparing strings on JsonPath filter, you need to include apostrophe (') around the value
  • in your input, values of "open" and "close" start with lowercase, so they won't match the JsonPath filter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants