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

Not to able to skip iteration in loop. #55

Closed
mdevale opened this issue Mar 18, 2019 · 7 comments · Fixed by #61
Closed

Not to able to skip iteration in loop. #55

mdevale opened this issue Mar 18, 2019 · 7 comments · Fixed by #61

Comments

@mdevale
Copy link

mdevale commented Mar 18, 2019

I want to transform Json that contains array. In one scenario, i want to skip iteration in loop. Is it possible to skip iteration on condition. As of now i want to skip first and last iteration

@Courela
Copy link
Contributor

Courela commented Mar 18, 2019

If you know array's length shorthand you can build a proper JsonPath expression that will filter the wanted positions, then apply it with #loop function:

Input:

{ "test": [ { "id": 1, "dummy": "Item1", "ignored": "bla" }, { "id": 2, "dummy": "Item2", "ignored": "bla" }, { "id": 3, "dummy": "Item3", "ignored": "bla" }, { "id": 4, "dummy": "Item4", "ignored": "bla" } ]}

Transformer:

{ "iteration": { "#loop($.test[1:3:1])": { "id": "#currentvalueatpath($.id)", "dummy": "#currentvalueatpath($.dummy)" } } }

Result:

{ "iteration": [ { "id": 2, "dummy": "Item2" }, { "id": 3, "dummy": "Item3" } ] }

@mdevale
Copy link
Author

mdevale commented Mar 18, 2019

Normally array's length is not fixed. It can be any number. Can i apply any condition with #currentIndex() or #lastIndex() in loop.

@Courela
Copy link
Contributor

Courela commented Mar 18, 2019

Those functions are only available inside a #loop function, so you will end up with something generated for first and last element of the array, which as I understand is what you don't want.

Besides what I mentioned, I don't see any other way of doing it as it stands.
One change that can be done to accommodate this it to allow having functions as argument for #loop function, so you can create the JsonPath expression dynamically. One will need a function to give the array's size, as described in #56.

@mdevale
Copy link
Author

mdevale commented Mar 18, 2019

$.test[1:3:1] what it indicates. How it will skip first and last iteration.

@Courela
Copy link
Contributor

Courela commented Mar 18, 2019

JsonPath Expressions

The first number is the start index (zero based), the second is the end index, the third is the step (optional)

@mdevale
Copy link
Author

mdevale commented Mar 19, 2019

Facing issue with the following scenario

Input:
{ "test": [ { "id": 1, "dummy": "Item1", "ignored": "bla" }, { "id": 2, "dummy": "Item2", "ignored": "bla" }, { "id": 3, "dummy": "Item3", "ignored": "bla" }, { "id": 4, "dummy": "Item4", "ignored": "bla" } ]}

Transformer:
{ "iteration": { "values": "#valueof($.test[*].dummy)" } }

Please see attached screenshot
image

I want following result:
{ "iteration" : { "values": ["Item1", "Item2", "Item3", "Item4"] } }

This is not able to achieve using #currentvalueatpath() also.

@Courela
Copy link
Contributor

Courela commented Mar 19, 2019

Currently #valueof function does not support multiple tokens as result, in other words, selecting multiple values and return the result as an array.

Pull request #48 was an attempt to solve similar problems (#46 ) by allowing #valueof function to handle multiple results, but it was reverted back by #50.

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

Successfully merging a pull request may close this issue.

2 participants