Search code examples
tsung

Extract array elements from a JSON response in tsung, array being the root of the response


I am using tsung 1.6 and I have a configuration file in which one of the requests return the following response.

Response:

[
  { key: value1 },
  { key: value2 }
]

My goal is to extract some variables items from the response and use them further. To be specific I would like to extract value1 and value2 in a dynamic variable. The following is what I am doing

<request>
  <dyn_variable name="value1" jsonpath="$[0].key"/>
  <dyn_variable name="value2" jsonpath="$[1].key"/>

  <http url='/get.json' version='1.1' method='GET'></http>
</request>

<request subst="true">
  <http
    url='/patch/%%_value1%%'
    version='1.1' 
    contents='{&quot;notified&quot;:true}'
    content_type='application/json;charset=utf-8'
    method='PATCH'
  />
</request>

The above is not giving me anything.

The $[0] syntax is something that I took from this site. This was referenced in the tsung documentation, however tsung also mentioned that it implements a very limited subset of the jsonpath.

Need help in extracting the following data.

Thanks


Solution

  • e.g.

    test.json: [{"a":1},{"b":2}]

    tsung.xml:

    <request>
    <dyn_variable name="Result" jsonpath="$.[0].a"/>
    <http url='/test.json' version='1.1' method='GET'></http>
    </request>
    

    p.s. The test.json file format is not utf-8 bom