Search code examples
jsonregexjmeterextractor

jmeter: I want to extract 2 values from 2 different json objects where 1 json object will validate my condition & other value I want to extract


My JSON Response is :

{
"results": [
    {
      "attributes": [
        {
          "format": "internal",
          "name": "resourceid",
          "type": "STRING",
          "value": "56B15190000015E85E57923F0000033B"
        },        
        {
          "format": "attribute",
          "name": "ds6w:identifier",
          "type": "string",
          "value": "ald7_al"
        }
      ]
    },
    {
      "attributes": [
        {
          "format": "internal",
          "name": "resourceid",
          "type": "STRING",
          "value": "56B15190000015E85E578B1F000001B6"
        },        
        {
          "format": "attribute",
          "name": "ds6w:identifier",
          "type": "string",
          "value": "fbh1"
        }
      ]
    },
    {
      "attributes": [
        {
          "format": "internal",
          "name": "resourceid",
          "type": "STRING",
          "value": "56B15190000015E85E578F7800000211"
        },
        {
          "format": "attribute",
          "name": "ds6w:identifier",
          "type": "string",
          "value": "u89cf"
        }
      ]
    }    
  ]
}

I want to get '56B15190000015E85E57923F0000033B' where value='ald7_al'

So basically within a jsonarray I have jsonobjects, and for single jsonobject I have two jsonobjects where secong jsonobject will validate my condition param and I want value from first jsonobject

For getting result to solve condition check I have used
JSON extractor expression as -> $..attributes[?(@.value==ald7_al)] which is giving me second json block but I want value from first json block.

Please help me if you have any inputs. Thanking you in advance for your help!


Solution

  • As of JMeter 5.2.1 this it not possible with built-in JMeter components

    So you're left with JSR223 PostProcessor and Groovy language, example code which should resolve your issue would be something like:

    def results = new groovy.json.JsonSlurper().parse(prev.getResponseData()).results
    
    0.upto(results.size() - 1, { index ->
        def attributes = results[index].attributes
        if (attributes[1].get('value').equals('ald7_al')) {
            vars.put('value', attributes[0].get('value'))
        }
    })
    

    Add it as a child of the request which returns the above JSON and if everything goes well you will able to access the value you're looking for as ${value} where required.

    More information: