Search code examples
javamaventestngcitrus-framework

Extract JSON Payload with Citrus Framework


I am trying to get some information from JSON responses using Citrus Framework, but I am not sure of how does the .extractFromPayload method works. For example, I make a GET request and the JSON response is:

["as:s.7635a8e95e1b707d","as:s.c585a8e92874e4a9","as:s.8935a8e9848d14ce","as:s.f885a8ebb1887346","as:s.0123456789abcdef","as:s.7b55a8e96a74893d","as:s.b3f5a8d9b3971120","as:s.5f05a8e936ed377d","as:s.6f5d3e05670a4b448bf9ba81d1172307","as:s.de07ada3dd724347a26fdd93231491e2","as:s.f705a8ebaa3e9e74"]

So the question is how can I get one of those keys? And if I need only the alphanumerical part, i.e 7635a8e95e1b707d, how can I parse it?.

Thank you very much!


Solution

  • The payload is a JSON array of Strings. You are able to access a value in that array using JsonPath expression and extractFromPayload method. After extracting the whole value as a test variable using JsonPath you may use a Citrus function like citrus:substring()or citrus:substringAfter() in a second operation.

    receive(someEndpoint)
        .extractFromPayload("$.[0]", "foo");
    
    echo("citrus:substringAfter(${foo} ,'.')");