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

Json transformation with conditions #1145

Open
hmmlopez opened this issue Apr 11, 2024 · 1 comment
Open

Json transformation with conditions #1145

hmmlopez opened this issue Apr 11, 2024 · 1 comment

Comments

@hmmlopez
Copy link

Citrus Version
Latest

Question
I have a case where we are retrieving a large json and we need to change some fields based on some conditions.
We have a cucumber test with a table, and when certain conditions of that table apply we need to add or replace some values in the JOSN message to be send by citrus to the SUT.

Example table (original table contains more):
A | B | C | D
1 | true | abc | false

A is always mapped to some field
So when B is true we need to change a field in the JSON to another constant value, if false leave it as is.

I would like to be able to manipulate the JSON-string before sending it to the TestCaseRunner so I don't need to use the process(jsonPath().expression()). The process does not allow me to apply on condition. So I'm missing that part of functionallity to change the JSON based on some conditions (condition apply to certain fields to be changed or not). In XSLT we were able to apply some transformation based on conditions.

What I've tried so far

Additional information
Would it be possible to do without having to parse the same JSON multiple times (for each condition)?

@hmmlopez
Copy link
Author

hmmlopez commented Apr 12, 2024

Should I use something like JsonPath from com.jayway.jsonpath:json-path ?
Bealdung has here more information about extracting but I see you can also manipulate the json, and can use predicates.

DocumentContext context = JsonPath.parse(text);
context = context.set("$.tool.jsonpath.creator.name", "Simon");
context = context.set("$.tool.jsonpath.creator.location[0]", "LA");
System.out.println(context.jsonString());

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

1 participant