0

I want to extract the exact string from the JSON response in Jmeter JSON extractor so below is the sample JSON payload-

    {   
"resource":[
    { 
        "id":"de04c6b1-a5a3-11ec-a02b-765e38f104a5-19",
        "name":"Tokyo-1-1-13",
        "service_name":"Tokyo-1-1-13_service",
        "oper_state":"UP",
        "type":"admin"
    },
    {
        "id":"me05c6b1-a903-11ec-a02b-764313f104a5-19",
        "name":"Tokyo-1-1-1",
        "service_name":"Tokyo-1-1-1_service",
        "oper_state":"UP",
        "type":"admin"
    },
    { 
        "id":"5e04c691-a5a3-11ec-a02b-765e38f3q4a5-19",
        "name":"Tokyo-1-1-14_service",
        "service_name":"Tokyo-1-1-14_service",
        "oper_state":"DOWN",
        "type":"admin"
    }
    ]}

So from the above JSON payload, I want to extract "oper_state" of a resource where "name":"Tokyo-1-1-1" and I'm using "$.resource[?(@.name=="Tokyo-1-1-1")].oper_state", but it is matching with the "Tokyo-1-1-13" and giving me the oper_state of this particular name, because "Tokyo-1-1-13" has "Tokyo-1-1-1" in it. Please help me to extract the exact data.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You would do better to use json functions than regex –  Mar 24 '22 at 06:18
  • Why would you use a regex for parsing json, in a tool that has a specific function for doing that? Or is the syntax used being misunderstood as a regex (`$.resource[?(@.name=="Tokyo-1-1-1")].oper_state` is not a regex)? – AD7six Mar 24 '22 at 06:53

1 Answers1

1

I cannot reproduce your issue:

enter image description here

You can double check yourself by putting the following JSON into i.e. Dummy Sampler:

{
  "resource": [
    {
      "id": "de04c6b1-a5a3-11ec-a02b-765e38f104a5-19",
      "name": "Tokyo-1-1-13",
      "service_name": "Tokyo-1-1-13_service",
      "oper_state": "1",
      "type": "admin"
    },
    {
      "id": "me05c6b1-a903-11ec-a02b-764313f104a5-19",
      "name": "Tokyo-1-1-1",
      "service_name": "Tokyo-1-1-1_service",
      "oper_state": "2",
      "type": "admin"
    },
    {
      "id": "5e04c691-a5a3-11ec-a02b-765e38f3q4a5-19",
      "name": "Tokyo-1-1-14_service",
      "service_name": "Tokyo-1-1-14_service",
      "oper_state": "3",
      "type": "admin"
    }
  ]
}

Also == filter operator assumes both operands are equal, it isn't any form of "substring" or "contains" or "matches", if you want to supply a regular expression - there is =~ operator

Dmitri T
  • 159,985
  • 5
  • 83
  • 133