17

I have following JSON format in response body

[
    {
        "Name" : "Prashant",
        "City" : "Sydney"
    },
    {
        "Name" : "Yogi",
        "City" : "London"
    }
]

What is the better way for checking if this array has any records and if yes give me "Name" for first array index. I am using jp@gc JSON extractor plugin for jMeter.

Is it possible to parse this using a plugin or do I need to do it using regular expressions?

brandonscript
  • 68,675
  • 32
  • 163
  • 220
prashant
  • 2,181
  • 2
  • 22
  • 37
  • **Does jmeter provide any programming language?** I've tried to find some info on it and **seems to be pure GUI thing.** – Tomas Jan 17 '14 at 18:21
  • If not, this question seems to be offtopic for SO. – Tomas Jan 18 '14 at 08:24
  • It has If, While, For, Switch controls but in a visual way, so it has a kind of GUI DSL – pmpm Feb 01 '14 at 19:54
  • If possible, always use `Regular Expression Extractor`. Try to avoid JSON / XPATH / Other extractors. They might look easy to use. But they consume more memory and time. It will affect the performance of your test plan. **source**: http://www.testautomationguru.com/jmeter-response-data-extractors-comparison/ – vins Nov 26 '16 at 18:08
  • See this complete guide explaining how to extract data from Json responses using the Json extractor: https://octoperf.com/blog/2017/03/09/how-to-extract-data-from-json-response-using-jmeter/ – Jerome L Apr 23 '18 at 08:55

6 Answers6

10

Using Ubik Load Pack JSON plugin for JMeter which is part of JMeter since version 3.0 (donated plugin) and called JSON Extractor, you can do it:

Test Plan overview:

enter image description here

ULP_JSON PostProcessor:

enter image description here

If Controller:

enter image description here

And here is the run result:

enter image description here

So as you can see it is possible with plain JMeter

If you're looking to learn JMeter, this book by 3 developers of the project will help you.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
6

I am not sure about your plugin but if it supports JSON path expressions it should be possible.
Try with this expression: $.[0].Name.

This is the plugin I use: http://jmeter-plugins.org/wiki/JSONPathExtractor/ and given expression works with it.

You can find more about JSON Path expressions here: http://goessner.net/articles/JsonPath/index.html#e2.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
MoD
  • 264
  • 2
  • 9
5

Working with JSON in JMeter is not quite easy as JMeter was designed long ago before JSON was invented. There are some extensions however that make life easier:

http://www.ubik-ingenierie.com/blog/extract-json-content-efficiently-with-jmeter-using-json-path-syntax-with-ubik-load-pack/

Hans
  • 505
  • 4
  • 4
  • Are you sure you tried our plugin (commercial), we don't have you as customer nor trial asker. YOu must be confusing with JMeter Plugins json plugin, see my answer below – UBIK LOAD PACK Jan 18 '14 at 22:43
  • 2
    How about a non-commercial solution? Ubik spams this site enough – Jaymes Bearden Mar 25 '14 at 19:01
  • Ubik has contributed its commercial plugin to Apache JMeter and it has been integrated as JSON Extractor. By the way Ubik-Ingenierie is a big contributor to ApacheJMeter since 10 versions as of June 2018, see http://jmeter.apache.org/changes.html#Thanks So your remark about spamming is not very nice or fair. Have a look at Top answerers and make your opinion. – UBIK LOAD PACK Jun 15 '18 at 07:46
3

We can add a regular expression extractor for fetching the value from the response.

Like This:

Regular expression extractor

Laurel
  • 5,965
  • 14
  • 31
  • 57
Shankar Daitha
  • 243
  • 5
  • 18
0

If possible, always use Regular Expression Extractor. Try to avoid JSON / XPATH / Other extractors. They might look easy to use. But they consume more memory and time. It will affect the performance of your test plan.

source: http://www.testautomationguru.com/jmeter-response-data-extractors-comparison/

vins
  • 15,030
  • 3
  • 36
  • 47
0

Rest Get service sample:

{
    "ObjectIdentifiers": {
        "internal": 1,
        "External1": "221212-12121",
        "External3": "",
        "Name": "koh"
    },
    "PartyType": "naturalPerson",
    "NaturalPerson": {
        "idNo": "221212-12121",
        "Title": "Mr",
        "Name": "koh",
        "FirstName": "",

We had a similar requirement in our project for parsing json responses using jmeter. The requirement was to validate all the fields in the json response and the expected values of field would be provided from external data source.

I found the JSR223 PostProcessor quite usefule in this case as we are able to implement Groovy scripts with this. it comes as a default plugin with the recent Jmeter version


Edit:

Below is the code snippet:

//get the JSON response from prev sampler
String getResponse = prev.getResponseDataAsString();

//parse the response and convert to string
JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
String parResponse = parser.parse(getResponse);

String preResponse = parResponse.toString();

JsonObject NaturalPerson = JsonObject.readFrom(preResponse);


//replace all commas with a semi-colon
String csvResponse = preResponse.replaceAll(",", ";");

//log response to file
logFileName = "C:/apache-jmeter-5.1.1/Web_Service_Output.csv";
BufferedWriter outLog = new BufferedWriter(new FileWriter(logFileName, true));
outLog.write(csvResponse + "\n");
outLog.close();
RobC
  • 22,977
  • 20
  • 73
  • 80
Sufi
  • 1
  • 3