In Jmeter when trying to extract data from Json object everything is ok and works great but, when i have array of Json objects can't extract it.
Result is:
[{"id":1,"name":"test"},{"id":2,"name":"test2"}]
it is my project JSON Extractor and JSR233 Assertion.
Inside Groovy script i'm making log.info but it doesn't captures variable value which is described inside JSON Extraxtor.
String id = vars.get("id");
log.info ("The example answer is " + id);
if (id == ""){
AssertionResult.setFailureMessage("The id is null");
AssertionResult.setFailure(true);
}
Please note that if Json response looks like this {"id":1,"name":"test"}
everything works correctly.
Change your JSON Path Expression to look like: $..id. .. is a deep scan operator so it will return all id attributes values.
Change Match No to -1
It will result in the following variables:
id_1=1
id_2=2
id_matchNr=2
I have no idea what exactly you need to assert, hopefully you will be able to amend your Groovy script yourself.
Also be aware that there is JSON Assertion test element available since JMeter 4.0 so you won't need to have separate extractor and assertion elements.
Related
I have a json object in my typescript which is returned from server side. One of the keys is dynamic. How do I parse and extract the value for that key.
Ex: serverResponse.detailsLMN.allList
Ex :serverResponse.detailsLMN.deleteList
In the above , 'LMN' is dynamic.
It can be serverResponse.detailsLMN.allList or serverResponse.detailsPQR.allList.
Assuming,
const temp = 'LMN' or 'PQR', how can I use temp to parse JSON object here.
Like : serverResponse.details{temp}.allList
Not sure if I understood your question correctly. But try doing
let data = JSON.parse(serverResponse);
((JSON.stringify(serverResponse)).includes("LMN")) ? serverResponse.detailsLMN.allList
: serverResponse.detailsPQR.allList
^The above code is the same as the if statement. If you don't know the ES6 ternary conditional statement then here's a link:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
To parse, simply use JSON.parse(). To extract the value, since I can't see the format of the output, it's better to console.log(serverResponse) the whole response and then walk through the Object in Chrome console to see how to get to your specific value.
I need to extract id where asOf : 2016-11 for latest run.
e.g given below response of request I need to store 21aa83fe-fe1b-4447-9b2e-05e7d6cd67d3 in a variable because as of 2016 is in the last and-and the corresponding id is. Could you please tell me how to write JSON Path Post Processor Expression out of it?
{"id":"1fabbb02-0b7d-4bec-badd-9fe9f1c63f08","batch" {"id":"GL1600"},"asOf":"2014-06-16T00:00:00Z","version":3,"statusIdentifier":"END"},
{"id":"212fc2b5-03d6-4d76-ab6d-e9e783459120","batch":{"id":"N1400"},"asOf":"2014-06-16T00:00:00Z","version":3,"statusIdentifier":"END"},
{"id":"21aa83fe-fe1b-4447-9b2e-05e7d6cd67d3","batch":{"id":"N1500"},"asOf":"2016-11-16T00:00:00Z","version":3,"statusIdentifier":"END"},
If your response is valid JSON you can use == filter operator like
$..[?(#.asOf == '2016-11-16T00:00:00Z')].id
References:
JSON Path - Filter Operators
Advanced Usage of the JSON Path Extractor in JMeter
If response is not valid JSON you will need to go for Regular Expression Extractor instead.
Sample input as below:
[{"program":"C:/temp/abc.exe","actions":"9","file_name":"abc1","new_file_name":"newabc1","version":"2.0.0.0","product_name":"abc","description":"abc","eventdate":"20160601120000"},{"program":"C:/temp/abc.exe","actions":"9","file_name":"abc2","new_file_name":"newabc2","version":"2.0.0.0","product_name":"abc","description":"abc","eventdate":"20160601120000"}]
I need to parse this json array into string for use in the following http request as parameters. I new to jmeter and tried to follow the example in http://theworkaholic.blogspot.my/2012/05/json-in-jmeter.html but i can't seem to get the value of jsonResponse at last.
[UPDATE]
[]1
Your question is not clear enough, however here are some clues:
If you need to store the whole response into a JMeter Variable for later re-use - go for Regular Expression Extractor configured as follows:
Reference Name: anythingMeaningful, i.e. jsonResponse
Regular Expression: (?s)(^.*)
Template: $1$
If you need 2 separate JSON Objects from the array - JSON Path PostProcessor is available since JMeter 3.0
$.[0] - for 1st JSON Array item
$.[1] - for 2nd JSON Array item
If you need anything else - use the same JSON Path PostProcessor. I would recommend getting familiarized with the following material:
JSONPath - XPath for JSON
Advanced Usage of the JSON Path Extractor in JMeter
Still no luck - update your question with exact details on how you need to re-use that JSON data in next request(s)
I'm not using JMeter too often, and I've run into very specific issue.
My REST response is always "the same", but nodes are not in the same order due to various reasons. As well, I can't put here whole response due to sensitive data, but let's use these dummy one:
First time response might be:
{
"properties":{
"prop1":false,
"prop2":false,
"prop3":165,
"prop4":"Audi",
"prop5":true,
"prop6":true,
"prop7":false,
"prop8":"1",
"prop9":"2.0",
"prop10":0
}
}
Then other time it might be like this:
{
"properties":{
"prop2":false,
"prop1":false,
"prop10":0,
"prop3":165,
"prop7":false,
"prop5":true,
"prop6":true,
"prop8":"1",
"prop9":"2.0",
"prop4":"Audi"
}
}
As you can see, the content it self is the same, but order of nodes it's not. I have 160+ nodes and thousand of possible response orders.
Is there an easy way to compare two JSON responses comparing matching key - values, or at least to sort the response, and then compare it with sorted one in assertion patterns?
I'm not using any plugins, just basic Apache JMeter.
Thanks
I've checked using Jython, you need to download the Jython Library and save to your jmeter lib directory.
I've checked 2 JSONs with Sampler1 and Sampler2, on Sampler1 I've add a BeanShell PostProcessor with this code:
vars.put("jsonSampler1",prev.getResponseDataAsString());
On Sampler2 I've add a BSF Assertion, specifying jython as the language and with the following code:
import json
jsonSampler1 = vars.get("jsonSampler1")
jsonSampler2 = prev.getResponseDataAsString()
objectSampler1 = json.loads(jsonSampler1)
objectSampler2 = json.loads(jsonSampler2)
if ( objectSampler1 != objectSampler2 ):
AssertionResult.setFailure(True)
AssertionResult.setFailureMessage("JSON data didn't match")
Yoy can find the whole jmx in this GistHub
You will most probably have to do this with a JSR223 Assertion and Groovy.
http://jmeter.apache.org/usermanual/component_reference.html#JSR223_Assertion
http://docs.groovy-lang.org/latest/html/api/groovy/json/JsonSlurper.html
Note that if you know Python, you might look at using Jython + JSR223.
I would just set up 10 jp#gc - JSON Path Assertions. Documentation for figuring out JSON Path format is here and you can test how it would work here.
For your example you would the assertion (Add > Assertion > jp#gc - JSON Path Assertions), then to test the prop 1 put:
$.properties.prop1
in the JSON Path field, click the Validate Against Expected Value checkbox, and put
false
in the expected value field. Repeat those steps for the other 9 changing the last part of the path to each key and the value you expected back in the expected value field.
This extractor is jmeter add on found here.
I'm trying to perform a JSON assertion using ATLANTBH jmeter JSON PATH Assertion. However I cant seem to write a correct expression to get the following fields from the JSON feed posted below:
123456789
1009
SOME RANDOM MESSAGE
{"api": {"status":"Success","callsremaining":36,"version":"x.x.x.x"}
,"result":{"errors":{"123456789":{"code":1009,"error":"SOME RANDOM MESSAGE"}}}
}
Has anyone here got any experience using this JMeter plugin?
I know I could use regex and Beanshell to validate but I'd rather use these JSON Path Assertion.
Any help you could provide would be most appreciated.
Looks like you can easily assert both 1009 and SOME RANDOM MESSAGE values using JSONPath expressions (in JSON Path Assertion components) but not sure about 123456789: that's not node value but bode name, and JSONPath implementation used by these components seems has no expressions to get node name.
Suppose you can easily use to assert 123456789 instead binding of JSON Path Extractor (from the same components collection) with jmeter's standard Response_Assertion.
Add 2 JSON Path Assertions as children to the sampler which returns json response you want to process:
Expressions will be $.result.errors..code and $.result.errors..error correspondingly.
Add JSON Path Extractor as child to the same sampler to extract full error entry:
Expression: $.result.errors..
This will extract {"123456789":{"error":"SOME RANDOM MESSAGE","code":1009}} and save into the pointed variable (${errorKey}).
Add Response Assertion as child to the same sampler, after previously added JSON Path Extractor:
This will assert name of the key (123456789) in the value of ${errorKey} variable.
So the final construction may look like
...
YOUR Sampler
JSON Path Extractor
JSON Path Assertion
JSON Path Assertion
Response Assertion
...