citrusframework / citrus

Framework for automated integration tests with focus on messaging integration
https://citrusframework.org
Apache License 2.0
456 stars 134 forks source link

Json transformation with conditions #1145

Open hmmlopez opened 5 months ago

hmmlopez commented 5 months ago

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 commented 5 months ago

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());