JsonPathVariableExtractor implements VariableExtractor { @Override public void extractVariables(Message message, TestContext context) { if (CollectionUtils.isEmpty(jsonPathExpressions)) {return;} if (log.isDebugEnabled()) { log.debug("Reading JSON elements with JSONPath"); } String jsonPathExpression; try { JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE); Object receivedJson = parser.parse(message.getPayload(String.class)); ReadContext readerContext = JsonPath.parse(receivedJson); for (Map.Entry<String, String> entry : jsonPathExpressions.entrySet()) { jsonPathExpression = context.replaceDynamicContentInString(entry.getKey()); String variableName = entry.getValue(); if (log.isDebugEnabled()) { log.debug("Evaluating JSONPath expression: " + jsonPathExpression); } Object jsonPathResult = JsonPathUtils.evaluate(readerContext, jsonPathExpression); if (jsonPathResult instanceof JSONArray) { context.setVariable(variableName, ((JSONArray) jsonPathResult).toJSONString()); } else if (jsonPathResult instanceof JSONObject) { context.setVariable(variableName, ((JSONObject) jsonPathResult).toJSONString()); } else { context.setVariable(variableName, Optional.ofNullable(jsonPathResult).orElse("null")); } } } catch (ParseException e) { throw new CitrusRuntimeException("Failed to parse JSON text", e); } } JsonPathVariableExtractor(); private  JsonPathVariableExtractor(Builder builder); @Override void extractVariables(Message message, TestContext context); Map<String, String> getJsonPathExpressions();  }
@Test public void testExtractVariables() throws Exception { JsonPathVariableExtractor variableExtractor = new JsonPathVariableExtractor.Builder() .expression("$['index']", "index") .expression("$.numbers", "numbers") .expression("$.numbers.size()", "numbersSize") .expression("$.person", "person") .expression("$.person.name", "personName") .expression("$.toString()", "toString") .expression("$.keySet()", "keySet") .expression("$.values()", "values") .expression("$.size()", "size") .expression("$.*", "all") .expression("$", "root") .build(); variableExtractor.extractVariables(jsonMessage, context); Assert.assertNotNull(context.getVariable("toString")); Assert.assertEquals(context.getVariable("toString"), "{\"person\":{\"surname\":\"Doe\",\"name\":\"John\"},\"numbers\":[10,20,30,40],\"index\":5,\"text\":\"Hello World!\",\"id\":\"x123456789x\"}"); Assert.assertNotNull(context.getVariable("keySet")); Assert.assertEquals(context.getVariable("keySet"), "[person, numbers, index, text, id]"); Assert.assertNotNull(context.getVariable("values")); Assert.assertEquals(context.getVariable("values"), "[{\"surname\":\"Doe\",\"name\":\"John\"}, [10,20,30,40], 5, Hello World!, x123456789x]"); Assert.assertNotNull(context.getVariable("size")); Assert.assertEquals(context.getVariable("size"), "5"); Assert.assertNotNull(context.getVariable("person")); Assert.assertEquals(context.getVariable("person"), "{\"surname\":\"Doe\",\"name\":\"John\"}"); Assert.assertNotNull(context.getVariable("personName")); Assert.assertEquals(context.getVariable("personName"), "John"); Assert.assertNotNull(context.getVariable("index")); Assert.assertEquals(context.getVariable("index"), "5"); Assert.assertNotNull(context.getVariable("numbers")); Assert.assertEquals(context.getVariable("numbers"), "[10,20,30,40]"); Assert.assertNotNull(context.getVariable("numbersSize")); Assert.assertEquals(context.getVariable("numbersSize"), "4"); Assert.assertNotNull(context.getVariable("all")); Assert.assertEquals(context.getVariable("all"), "[{\"surname\":\"Doe\",\"name\":\"John\"},[10,20,30,40],5,\"Hello World!\",\"x123456789x\"]"); Assert.assertNotNull(context.getVariable("root")); Assert.assertEquals(context.getVariable("root"), "{\"person\":{\"surname\":\"Doe\",\"name\":\"John\"},\"numbers\":[10,20,30,40],\"index\":5,\"text\":\"Hello World!\",\"id\":\"x123456789x\"}"); }