-2

I have a list of map (parsed from json output of a rest request) like

[[Mobile:9876543210, Name:ABCD], [Mobile:8765432109, Name:EFGH], [Mobile:7654321098, Name:IJKL], [Mobile:6543210987, Name:MNOP]] 

Original JSON was like

{
    "data": [{
            "Name": "ABCD",
            "Mobile": "9876543210"
        },
        {
            "Name": "EFGH",
            "Mobile": "8765432109"
        },
        {
            "Name": "IJKL",
            "Mobile": "7654321098"
        },
        {
            "Name": "MNOP",
            "Mobile": "6543210987"
        }
    ]
}

I want to get the mobile value from the name

Tried some things but just not working out.

Trying this in JMETER JSR223 post processor using Groovy.

Rao
  • 20,781
  • 11
  • 57
  • 77
Suhas Deshpande
  • 183
  • 2
  • 3
  • 15
  • use `collectEntries` to build a map from it, where you have name as the key and the mobile as the value. so you can use that map to lookup the mobile for a name. if you only have to do this once you can also use `findResult`. other than that, please add what you have tried so far and what problems you got with your apporach. – cfrick Apr 14 '17 at 13:08
  • For future, please don't say "tried some things" - name and describe them. – Yuri G Apr 14 '17 at 20:58

2 Answers2

1

You should be able to get the Mobile based on Name.

Below code fetches the Mobile 8765432109 when Name is EFGH from the OP's data. Similarly you can change the value of Name to get the right Mibile.

//Pass jsonString value to below parseText method
def json = new groovy.json.JsonSlurper().parseText(jsonString)
def result = json.data.find { it.Name == 'EFGH' }.Mobile
println result

You can quickly try online Demo

Rao
  • 20,781
  • 11
  • 57
  • 77
0

Here is an example Groovy code to fetch Name:Mobile pairs from the original JSON response (use the code in the JSR223 PostProcessor)

def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())

json.data.each {entry ->
    entry.each {k, v -> log.info("${k}:${v}")}
}

Demo:

Groovy JSON Example

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks. That would be a good way to process JSON when I need to use each of the values (mobile and name) in the list. For now I just need one of them. – Suhas Deshpande Apr 17 '17 at 05:49