Jmeter JSON Path Assertion - json

Below is my JSON response data, I need to do assertion using the below response. I tried in many ways to write JSON path & Expected Value. Always assertion fails. What I want is please help to write the path and expected value for the below data
{
"statusCode": 200,
"statusMessage": "Success",
"errorMessage": "",
"responseData": {
"id": 15,
"userName": "partner#tarento.com",
"firstName": "tarento",
"lastName": "test1",
"phoneNumber": "1234567812",
"email": "partner#tarento.com",
"password": "",
"city": "",
"agentList": [
{
"id": 37,
"userName": "Rahul.antonyRaj#tarento.com",
"firstName": "Sanjay",
"lastName": "rahul",
"phoneNumber": "7411269480",
"email": "Rahul.antonyRaj#tarento.com",
"password": "",
"active": true
},
{
"id": 68,
"userName": "jinesh.sumedhan#tareto.com",
"firstName": "jinesh",
"lastName": "sumedhan",
"phoneNumber": "9400993826",
"email": "jinesh.sumedhan#tareto.com",
"password": "",
"active": true
},
{
"id": 108,
"userName": "a.sanjayrahul#gmail.com",
"firstName": "Rahul",
"lastName": "Antony",
"phoneNumber": "9994590241",
"email": "a.sanjayrahul#gmail.com",
"password": "",
"active": true
},
{
"id": 304,
"userName": "a.sanjayrajish#gmail.com",
"firstName": "Agent",
"lastName": "Agent",
"phoneNumber": "9025699716",
"email": "a.sanjayrajish#gmail.com",
"password": "",
"active": true
}
],
"roleName": "admin",
"sessionKey": "435tnerLt9813942160478oDse46345635#1",
"partner": {
"id": 1,
"name": "Tarento",
"cityList": [
"bangalore",
"mumbai"
],
"phone": "1234567812",
"url": ""
},
"isActive": true,
"isDeleted": false,
"roleId": 1,
"countryCode": "",
"tags": [
{
"tagId": 1,
"name": "all",
"description": "this is default tag of all driver."
},
{
"tagId": 2,
"name": "airport",
"description": ""
},
{
"tagId": 3,
"name": "street",
"description": "any text message"
},
{
"tagId": 255,
"name": "night",
"description": "night"
}
]
}
}
I received the following response

For start following JSONPath Assertion will test your statusCode
$.statusCode
put 200 to Expected Value of JSONPath Assertion.
This one is for userName
$.responseData.userName
Easy, isn't it? See Parsing JSON guide for more useful examples and how-tos.

I found the JSR223 Assertion with script language javascript to be the easiest. at least if you have knowledge in java and javascript. And no need to add any plugins.
My working code in detail:
var json = JSON.parse(SampleResult.getResponseDataAsString());
if (json.statusCode != 200) {
AssertionResult.setFailureMessage(""
+ json.statusCode
+ " " + json.statusMessage
+ " " + json.errorMessage);
AssertionResult.setFailure(true);
}

I personally prefer to use BSF PostProcessor in coupling with Groovy language. Example of how to parse JSON with Groovy you can find here how to parse json using groovy

Related

Accessing the Data in json object

This Works
app.get('/api/v1/admin/getAllUsers',async function (req,res,next){
try {
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ') || !req.headers.authorization.split(' ')[1]) {
return res.status(422).json({ message: 'Please Provide Token!' })
}
dbConn.query('select * from fasta_users; select * from fasta_admin_users;select Name, email,password,phone,role,status,created_at from fasta_riders;', function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: results , message: 'users list' });
});
} catch (err) {
next(err);
}
})
It returns the data i want without problems but the Json is Looking like this
{
"error": false,
"data": [
[
{
"id": 1,
"fastaUserId": "",
"fullname": "John Qasim",
"email": "j_qasim001#aol.com",
"password": "$2a$12$iAzLsIEcOJpDSSgdUmJBR.e8pWdieUg7N4bFiW0lkgBsQQDA6DwPG",
"state": "Imo",
"role": "Customers",
"city": "Owerri",
"phone_num": "08076631261",
"created_at": "2022-05-17T14:58:26.000Z"
},
{
"id": 2,
"fastaUserId": "FU-71198",
"fullname": "z",
"email": "z#gmail.com",
"password": "$2a$12$a9ibquFv477e6KL3N49JweVoiSmDXR0UVcM5xCkB39/8loKZ/u0bq",
"state": "z",
"role": "z",
"city": "user",
"phone_num": "z",
"created_at": "2022-05-17T17:57:39.000Z"
}
],
[
{
"id": 1,
"fastaUserId": "",
"fullname": "John Legend",
"email": "john.legend#gmail.com",
"password": "$2a$12$KrW0YKsJmvlp2z5uF4V10uLIjcPpSl1ba/LiWuiRpaLs/XRmguXDy",
"phone_num": "0188272610",
"avatar": "test_pic",
"role": "Administrator",
"status": "Active",
"created_at": "2022-05-11T17:48:39.000Z"
},
{
"id": 2,
"fastaUserId": "FA-76249",
"fullname": "James Hall",
"email": "jameshallblog.com#gmail.com",
"password": "$2a$12$j711FBY441CHwacisjcL9OT9Rjb5Yll8VzdSncKJilC0Pku9i2RP2",
"phone_num": "090865735422",
"avatar": "https://oxfordssocustomerapi.azurewebsites.net/Docs/Users/OIG-00424.jpg",
"role": "Administrator",
"status": "Active",
"created_at": "2022-05-17T17:51:03.000Z"
}
],
[
{
"Name": "Karen Mitchelle",
"email": "k.m.boon#gmail.com",
"password": "$2a$12$PCTK8okWO/ApoNSgXXTIl.oxzwqf.zlHHJaEvxtfxpUrnI1IaGNQC",
"phone": "08128829102",
"role": "Rider",
"status": "Active",
"created_at": "2022-05-17T14:59:22.000Z"
}
]
],
"message": "users list"
}
But I do not want this ^^^ above as a Response. I want something like this instead.
"data":[{
"id": 1,
"fastaUserId": "",
"fullname": "John Qasim",
"email": "j_qasim001#aol.com",
"password": "$2a$12$iAzLsIEcOJpDSSgdUmJBR.e8pWdieUg7N4bFiW0lkgBsQQDA6DwPG",
"state": "Imo",
"role": "Customers",
"city": "Owerri",
"phone_num": "08076631261",
"created_at": "2022-05-17T15:58:26.000Z"
},
{
"id": 1,
"fastaUserId": "",
"fullname": "alex roman",
"email": "alex.roman#juno.com",
"password": "$2a$12$sXpfYaQQIwoBf1KMaPrgT.PioHY4Y6SXD1lpheDB9EPTwfRtsg1p.",
"state": "Lagos",
"city": "city",
"phone_num": "08111111111",
"company_type": "Registered",
"company_name": "Polaris Shipping Inc.",
"company_regnum": "RC-123456",
"c_license": 1,
"role": "Delivery man",
"l_licensenumber": "LAG-2372328",
"company_address": "150 Wole Madariola Close",
"num_employees": "200",
"company_nature": "Sole Propietorship",
"created_at": "2022-05-17T15:57:41.000Z"
},
{
"id": 1,
"fastaUserId": "",
"fullname": "John Legend",
"email": "john.legend#gmail.com",
"password": "$2a$12$KrW0YKsJmvlp2z5uF4V10uLIjcPpSl1ba/LiWuiRpaLs/XRmguXDy",
"phone_num": "0188272610",
"avatar": "test_pic",
"role": "Administrator",
"status": "Active",
"created_at": "2022-05-11T18:48:39.000Z"
},
{
"id": 1,
"Name": "Karen Mitchelle",
"RidersID": "FR-60842",
"Status": "Active",
"email": "k.m.boon#gmail.com",
"password": "$2a$12$PCTK8okWO/ApoNSgXXTIl.oxzwqf.zlHHJaEvxtfxpUrnI1IaGNQC",
"phone": "08128829102",
"No_deliveries": "200",
"rating": "5",
"state": "Lagos",
"city": "Lagos",
"role": "Rider",
"address": "150 Wole Kolawole Str. Lagos Nigeria",
"bike_manufacturer": "Suzuki",
"bike_model": "Suzuki 400",
"bike_year": "2012",
"engine_power": "HP8",
"bike_color": "red",
"license_number": "SMK900-LA",
"license_expiry": "2027-05-01",
"vehicle_id": "LA8373Q2",
"created_at": "2022-05-17T15:59:22.000Z"
}
]
What must i do in my code above to access that data object from results in the response here like this return res.send({ error: false, data: results , message: 'users list' }); and return something like the second Json as shown above? I am trying to get this solved and nothing i have seen on the internet is being quite helpful here. Please i need help

Json parsing losgstash

i can't parse this json with logstash... someone could help me?
seems like the way it is parsed can't be readed by logstash.
there is a ruby code to parse this?
I cannot extract the fields nested in the square brackets
[
{
"capacity": 0,
"created_at": "2021-04-06T16:18:34+02:00",
"decisions": [
{
"duration": "22h16m4.141220361s",
"id": 842,
"origin": "CAPI",
"scenario": "crowdsecurity/http-bad-user-agent",
"scope": "ip",
"simulated": false,
"type": "ban",
"value": "3.214.184.223/32"
},
.
.
.
{
"duration": "22h16m4.195897491s",
"id": 904,
"origin": "CAPI",
"scenario": "crowdsecurity/http-backdoors-attempts",
"scope": "ip",
"simulated": false,
"type": "ban",
"value": "51.68.11.195/32"
}
],
"events": null,
"events_count": 0,
"id": 12,
"labels": null,
"leakspeed": "",
"machine_id": "N/A",
"message": "",
"scenario": "update : +63/-0 IPs",
"scenario_hash": "",
"scenario_version": "",
"simulated": false,
"source": {
"scope": "Community blocklist",
"value": ""
},
"start_at": "2021-04-06 16:18:34.750588276 +0200 +0200",
"stop_at": "2021-04-06 16:18:34.750588717 +0200 +0200"
}
]
Require JSON
JSON.parse(yourString)
Would likely be what you're looking for.
The module is described here

Jmeter: JSON response manipulation and passing to the next http request

I've got the response from HTTP GET request as JSON file and I want to use that JSON and pass it to the next HTTP request. I got the following response data
{
"apiInfo": {
"id": "23143",
"name": "bookkeeping",
"state": "used",
"data": "15893712000000"
},
"apiDetails": [
{
"bookName": "abc",
"state": "old",
"noOfTimesUsed": "53"
"additionalParam"{
"name": "abc",
"id": "123"
}
},
{
"bookName": "def",
"state": "new",
"noOfTimesUsed": "5",
"action": "keep"
"additionalParam"{
"name": "def",
"id": "456"
}
},
{
"bookName": "xyz",
"state": "avg",
"noOfTimesUsed": "23"
"additionalParam"{
"name": "ghi",
"id": "789"
}
},
{
"bookName": "pqr",
"state": "old",
"noOfTimesUsed": "75",
"action": "discard"
"additionalParam"{
"name": "jkl",
"id": "012"
}
}
]
}
I want to use "apiInfo" & "apiDetails" part from the JSON response and manipulate its data. As you can notice, some array field have attribute "action" in it and some one doesn't. I want to make sure all the field in the array have this data and is assigned as ' "action":"keep" '. Also, I want to add "id" from apiInfo & "name" from additionalParams from apiDetails itself. The end result I want is somewhat like this
"apiDetails": [
{
"id": "23143",
"bookName": "abc",
"state": "old",
"noOfTimesUsed": "53",
"action": "keep",
"name":"abc"
},
{
"id": "23143",
"bookName": "def",
"state": "new",
"noOfTimesUsed": "5",
"action": "keep",
"name":"def"
},
{
"id": "23143",
"bookName": "xyz",
"state": "avg",
"noOfTimesUsed": "23",
"action": "keep",
"name":"ghi"
},
{
"id": "23143",
"bookName": "pqr",
"state": "old",
"noOfTimesUsed": "75",
"action": "keep",
"name":"jkl"
}
]
I've been trying to use JSR223 sampler and have been struggling with it. It's bit complicated and I need help. P.S.: I've tried using javascript code to manipulate the results as desired but have been unsuccessful.
Please help.
Thanks, Sid
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def apiDetails = new groovy.json.JsonSlurper().parse(prev.getResponseData()).apiDetails
apiDetails.each { apiDetail ->
apiDetail.put('action', 'keep')
}
vars.put('request', new groovy.json.JsonBuilder(apidetails: apiDetails.collect()).toPrettyString())
That's it, you should be able to refer the generated request as ${request} where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

angularJS JSON parsing

I am new to angularJS and trying to parse the data and display it on a page.
{
"count": 13,
"status": 200,
"statusMessage": "OK",
"userContact": [
{
"user": {
"id": 1,
"firstName": "test",
"lastName": "test",
"universityId": 1,
"email": "test#harbingergroup.com",
"password": "",
"phoneNo": "1234567890",
"gender": "M",
"userType": 1,
"medicalComments": "",
"dob": "2015-02-22",
"universityAffiliation": 1,
"cityId": "1"
},
"userContactList": {
"count": 2,
"status": 200,
"statusMessage": "OK",
"contact": [
{
"id": 2,
"userId": 1,
"firstName": "test",
"lastName": "user",
"phoneNo": "9876543210",
"email": "test#example.com"
},
{
"id": 24,
"userId": 1,
"firstName": "first1",
"lastName": "last1",
"phoneNo": "9876543210",
"email": "test#example.com"
}
]
}
}]
}
this is my JSON data.What i am trying is :
<tr ng-repeat="studentDetails in studentProfileData">
studentDetails.status for getting status and studentDetails.userContacts.firstName for first name
but I am not getting the data. What is wrong here?
Assuming that studentProfileData contains a list of users (userContact):
studentDetails.user.firstName
will contain "test"

How do i check my Json

I'm using Titanium Appcelerator to develop an Android application..I getting an error while using 'ACS JSON' .. I trying to use posts query, But i getting different kinds of array ..i need to check is whether the string is available or not before access..
For Example:
"response": {
"posts": [
{
"id": "52e7800340b4b0aa134",
"title": "test",
"created_at": "2014-01-28T10:01:39+0000",
"updated_at": "2014-01-30T11:59:54+0000",
"content": "#hi all",
"reviews_count": 3,
"ratings_count": 3,
"ratings_average": 3.67,
"ratings_summary": {
"5": 1,
"3": 2
},
"user": {
"id": "52e5e87f08a3e70b3309c3e3",
"first_name": "aa",
"last_name": "ss",
"created_at": "2014-01-27T05:02:55+0000",
"updated_at": "2014-01-30T11:58:49+0000",
"external_accounts": [
],
"confirmed_at": "2014-01-27T05:02:55+0000",
"username": "ss",
"role": "a",
"admin": "false"
},
"custom_fields": {
"postedby": "aa",
}
},
{
"id": "52e7908a3e70b3d0a9614",
"title": "bb",
"tags": [
"sdf",
],
"created_at": "2014-01-28T11:46:00+0000",
"updated_at": "2014-01-30T11:09:17+0000",
"content": "#hi# #kWh v #sdf",
"user": {
"id": "52e5e87f08a3e70b3309c3e3",
"first_name": "bb",
"last_name": "bbc",
"created_at": "2014-01-27T05:02:55+0000",
"updated_at": "2014-01-30T11:58:49+0000",
"external_accounts": [
],
"confirmed_at": "2014-01-27T05:02:55+0000",
"username": "b",
"role": "b",
"admin": "false"
},
"custom_fields": {
"postedby": "b"
}
},
See I getting *ratings_count* in my first post..while trying access this inside for loop i getting error..
Now i need to check is whether the string is available or not before access..!
In titanium you can check anything whether it exist or not by using if statment.You can do this
if(ratingcount){
//Do here whatever you want to do
}
Thanks
Try:
for (var i in response.posts) {
post = response.posts[i];
if (post.ratings_count) {
/* Do operations for post with ratings */
} else {
/* Do operations for post without ratings */
}
}
This is pretty basic operation in JavaScript, so before you dive deeper into Titanium it's good to catch up with plain JS before.