JMeter - Extract value from the variable having JSON data - json

I am getting following response in one GET API call.
{
"id": 78929,
"created": "04-12-2021 02:37:52",
"modified": "04-12-2021 02:37:52",
"name": "Applebee",
"url": "https://applebee-2.prashant.com",
"key1": "vp_9d254bb754ab6283afa9ecf0f0847b5a",
"interval2": 120,
"tenant_id": 60895,
"channel_id": 2,
"channel_type": "platform",
"warehouse_id": 54533,
"warehouse_name": "Bedfordview Warehouse",
"stock_policy": "CUSTOM",
"currency_id": 45396,
"order_from_date": "2021-10-03 02:36:48",
"inventory_update": true,
"order_update": true,
"price_update": true,
"active": true,
"resync": true,
"last_access_record_id": 89880,
"last_accessed_record": {
"id": 89880,
"last_order_to_date": "2021-12-07T00:16:39.000+0000",
"last_order_id": "0",
"last_order_page": 1
},
"store_config": "{\"isOrderUpdateProcessor\":false,\"auto_archive\":true,\"tagOrderRisks\":true,\"error_emails\":\"kyle#gmail.com\",\"autoMapDuplicateProduct\":true,\"enabledMultiCurrency\":false,\"disableSyncPIIData\":false}",
"last_inventory_sync": "04-12-2021 02:43:35",
"inventory_sync_interval": 1440,
"stock_policy_warehouse_ids": [
{
"id": 53543,
"created": "04-12-2021 02:37:52",
"modified": "04-12-2021 02:37:52",
"store_id": 78929,
"external_warehouse_id": "34639577171",
"warehouse_id": 54533,
"add_qty_value": "0%",
"add_incoming_qty_value": "0%",
"tenant_id": 60895
},
{
"id": 53544,
"created": "04-12-2021 02:37:52",
"modified": "04-12-2021 02:37:52",
"store_id": 78929,
"external_warehouse_id": "60921610323",
"warehouse_id": 54534,
"add_qty_value": "0%",
"add_incoming_qty_value": "0%",
"tenant_id": 60895
}
],
"old_shopify_locations": [
{
"id": "60921610323",
"name": "Supplier",
"warehouseId": 54534,
"warehouseName": "Lux"
},
{
"id": "34639577171",
"name": "Bedfordview Warehouse",
"warehouseId": 54533,
"warehouseName": "Bedfordview Warehouse"
}
]
}
I have added following JSON Extractor to fetch store_config value from the above API response.
So as per above example, I am getting following store_config value via JSON Extractor.
{
"isOrderUpdateProcessor": false,
"auto_archive": true,
"tagOrderRisks": true,
"error_emails": "kyle#gmail.com",
"autoMapDuplicateProduct": true,
"enabledMultiCurrency": false,
"disableSyncPIIData": false
}
So Now, I want to fetch "isOrderUpdateProcessor" value from the above store_config variable.
So please help me into this.

You can use another JSON Extractor to extract from your variable by choosing the radio button JMeter variable and add your expression as$.isOrderUpdateProcessor and save result in a new variable

If you want to extract isOrderUpdateProcessor from the result
Add a JSON Extractor just below you JSON extractor
Configure the JSON Extractor to extract the value isOrderUpdateProcessor from the JMeter variable store_config

Given you have groovy in your tags it would be something like:
vars.put('foo', new groovy.json.JsonSlurper().parseText(vars.get('StoreConfig')).isOrderUpdateProcessor as String)
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Related

Not able to retrieve value in JSON response

I am working on API response validation. I am trying to get the value of the particular field in the API response for validation.
This is my API response.
{
"currentdatetime": "11",
"syncinterval_seconds": 4,
"public_keys": [
{
"system": "aa",
"pubkey": "aa"
}
],
"users": [
{
"username": "user",
"user_id": "user",
"realname": "user_10",
"emailaddress": "test#gmail.com",
"accountenabled": false,
"mustchangepassword": false,
"passwordhash": "$E6gi",
"accesslevel": [
"WORKER"
],
"expiration": "2022-11-02T16:21:52",
"site_specific": false,
"symkey": "aaaa",
"privatekey": "aaa"
},
]
}
I am trying to get username /user id from users through robot code.Like this
${response}= GET ${Base_URL}/v1/sync/users ${data} headers=${header}
${origin}= Get Json Value From String ${response.content} users
${lines} = Set Variable ${origin.content['users']['username']}
It is throwing a "type" error.
Any help will be much appreciated.

Jmeter - Using JSON Extractor for getting variables with the same name

This is my JSON:
[
{
"rooms": [
{
"name": "Name1",
"id": 148,
"isActive": true,
"properties": {
}
},
{
"name": "Name 2",
"id": 149,
"isActive": true,
"properties": {
}
},
{
"name": "Name 3",
"id": 150,
"isActive": true,
"properties": {
}
}
],
"timezone": "America\/New_York",
"name": "AnotherName",
"id": 88,
"isActive": false,
"properties": {
}
}]
In order to extract the "timezone" field I use: "$..timezone" and in return I get:
Result[0]=America/New_York
Using "$..id" will return:
Result[0]=88
Result[1]=148
Result[2]=149
Result[3]=150
The question is: Which syntax to use in order to extract only Result[0]=88 ?
I've been trying several available options but with no success.. Any ideas?
You're using .. operator which means deep scan so JMeter finds all id attributes and returns all their values.
If you want only the top-level ID for the first entry in the response JSON Array you need to explicitly set path to it like:
$.[0].id
if you need to get the id where timezone is America/New_York - the relevant JSON Path query would be:
$..[?(#.timezone == 'America/New_York')].id
More information: JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios
You can use $.[*]id to get Result[0]=88

Concatenating lists in Groovy

I have captured two sets of values by using JSON extractor in JMeter which I want to concatenate. Let me give you an example below for the format which I want to use.
The following are the two sets of captured values:
Set 1: [V2520 V2522 V2521 V2500 V2500]
Set 2: [PL PL PL NP NP]
So from the above sets, I am looking for the something like the following value, because the body which I have to send in a subsequent call contains the combination of these 2 values:
Answer: ["V2520PL", "V2522PL", "V2521PL", "V2500NP", "V2500NP"]
Can you please help me how to solve this in JMeter using Groovy?
This is the JSON I have:
{ "body": {
"responseObject": [
{
"benefitInfo": [
{
"procedureCode": "V2520",
"modifier": "PL",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensDisposable",
"id": "96",
"coPayAmount": "25"
},
{
"procedureCode": "V2522",
"modifier": "PL",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensDisposableBifocal",
"id": "98",
"coPayAmount": "25"
},
{
"procedureCode": "V2521",
"modifier": "PL",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensDisposableToric",
"id": "97",
"coPayAmount": "25"
},
{
"procedureCode": "V2500",
"modifier": "NP",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLens (Non Plan)",
"id": "89",
"coPayAmount": "0"
},
{
"procedureCode": "V2500",
"modifier": "NP",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensConventional (Non Plan)",
"id": "157",
"coPayAmount": "0"
}
]
}
]}}
An easy way to do this is to combine them as you collect the values from the JSON when you parse it.
def json = new groovy.json.JsonSlurper().parseText(text)
def answer = json.body.responseObject[0].benefitInfo.collect { it.procedureCode + it.modifier }
assert answer == ["V2520PL", "V2522PL", "V2521PL", "V2500NP", "V2500NP"]
Another method would be to use transpose() and join():
def r = new groovy.json.JsonSlurper().parseText(text).body.responseObject.benefitInfo[0]
def answer = [r.procedureCode, r.modifier].transpose()*.join()
assert answer == ["V2520PL", "V2522PL", "V2521PL", "V2500NP", "V2500NP"]
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def answer = []
def benefitInfos = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..benefitInfo')
benefitInfos.each { benefitInfo ->
benefitInfo.each { entry ->
answer.add(entry.get('procedureCode') + entry.get('modifier'))
}
}
vars.put('answer', new groovy.json.JsonBuilder(answer).toPrettyString())
That's it, you will be able to access generated value as ${answer} where required:
References:
Jayway JsonPath
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

JSON object extract from big query database using app script

I have a big query table that has a JSON object as one of the field in the table. How can I extract the data from the JSON object using app script. The object itself is nested. It looks like this
{
"uid": "124551",
"subjects": [
{
"tid": 37,
"title": "Algebra",
"html_id": "algebra",
"selected": true
},
{
"tid": 214853,
"title": "Trigonometry",
"html_id": "trigonometry",
"selected": true
},
{
"tid": 38,
"title": "Geometry",
"html_id": "geometry",
"selected": true
}
],
"cellphone": "09178854579",
"educations": [
{
"index": 0,
"schoolname": "University of the Philippines - Los BaƱos",
"degree": "BS Mathematics",
"major": "Mathematics",
"eduFrom": "2009-05-31T16:00:00.000Z",
"eduTo": "2013-04-26T16:00:00.000Z",
"eduFromTs": 1243785600,
"eduToTs": 1366992000
}
],
"info": {
"os": "Windows",
"internet": "ADSL",
"browser": "Chrome",
"network": "Wireless",
"speed": "",
"timezone": "Asia/Hong_Kong"
}
}
I want to extract all school names from education field. Any ideas?
Working with JSON objects is similar to working with XML, except that parsing or encoding a JSON object is much easier.
Once this string is retrieved, simply call JSON.parse() on the string to get a native object representation.
var data = JSON.parse(json);
Browser.msgBox(data.info.os);
Other sample code is at https://developers.google.com/apps-script/advanced/bigquery#run_query

Backbone.js Collections

I am working with backbone.js. I am trying to send a request to restful service i am getting the resultset as json object as shown
{
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}.
I am putting the json object extracting and setting it to collection but I am not able to get particular model from the collection with specific id.
If you want to make your model can be identified with id. You have to set the id in the attributes hash:
{
"id": 1001,
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}
Then using backbone collection "findWhere" method to get the specific id model.
//assume msgCollection is which you put the models
var model = msgCollection.findWhere({id: 1001});
Hope this is helpful for you.
I think you need to set your model and parse your input data. Checkout http://backbonejs.org/#Collection-model, http://backbonejs.org/#Model-parse and http://backbonejs.org/#Collection-parse.