JsonPath v.2.7.0
Using this code does not print expecting string
String jsonActual = "[" +
"{\"count\":15,\"_id\":{\"DB User Name\":\"admin\",\"Session Activity Type\":\"LOGOFF\"}}," +
"{\"count\":15,\"_id\":{\"DB User Name\":\"admin\",\"Session Activity Type\":\"LOGON\"}}" +
"]";
Predicate predicate = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
String value = ctx.item(Map.class).get("DB User Name").toString();
return value.equals("dbuser");
}
};
String updatedActual = JsonPath.parse(jsonActual).set("$..['DB User Name']", "updated-Admin", predicate).jsonString();
System.out.println(updatedActual);
I would like to update 'DB User Name' field value globally with a condition - only when its value equal to "dbuser".
Actual output is:
[{"count":15,"_id":{"DB User Name":"updated-Admin","Session Activity Type":"LOGOFF"}},{"count":15,"_id":{"DB User Name":"updated-Admin","Session Activity Type":"LOGON"}}]
Expected output: the json-string should not be updated since the specified predicate does not match its criteria.