Postman test save variable from JSON response body - postman

I have json response body like this
{
"header": {
"process_time": "27.842975",
"server_time": "",
"reason": "",
"error_code": 0,
"status_code": 200
},
"data": {
"idx": "2",
"component": {
"id": 123,
"name": "product",
"properties": {
"button_notification": false
},
"title": "",
"data": [
{
"status": 4,
"product_id": 1112323,
"stock": 21
},
{
"status": 4,
"product_id": 1114322,
"stock": 13
},
{
"status": 4,
"stock": 8,
"product_id": 1115342
},
{
"stock": 5,
"status": 4,
"product_id": 1115234
},
{
"stock": 5,
"status": 4,
"product_id": 1115442
}
]
}
},
"errors": []
}
I want to save two product_id values which have the most two stock than other
here my code
var jsonData = JSON.parse(responseBody);
let productId-1 = jsonData.data.component.data[2].product_id
let productId-2 = jsonData.data.component.data[3].product_id
pm.environment.set("productId-1",productId-1)
pm.environment.set("productId-2",productId-2)

The most simple solution to your problem would be implementing bubble sort for finding the product with the most stock.
let arr = jsonData.data.component.data;
for(let i = 0; i < arr.length - 1; i++){
for(let j = 0; j < arr.length - 1 - i; j++){
if (arr[j].stock < arr[j+1].stock) {
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
pm.environment.set("productId-1", arr[0]); //product with most stock
pm.environment.set("productId-2", arr[1]); //product with second most stock

Related

Elasticsearch query to match a pattern and replace it with regex

I have to get the TOP 5 hit APIs/Urls and display with the http statuses of each url and I am able to do it in a way with the below extraction query
{
"query": {
"bool": {
"must": [
{
"range": {
"#timestamp": {
"from": "now-15m",
"to": "now",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"aggregations": {
"Url": {
"terms": {
"field": "data.url.keyword",
"size": 5,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"Status": {
"terms": {
"field": "data.response.status",
"size": 5,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
}
}
Output :
"aggregations": {
"Url": {
"doc_count_error_upper_bound": 940,
"sum_other_doc_count": 52374,
"buckets": [
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3,
"key": 200
},
{
"doc_count": 254,
"key": 400
}
]
},
"doc_count": 3515,
"key": "/account/me/"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3376,
"key": 200
}
]
},
"doc_count": 3385,
"key": "/PlanDetails"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3282,
"key": 200
}
]
},
"doc_count": 3282,
"key": "/evaluation"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3205,
"key": 200
}
]
},
"doc_count": 3205,
"key": "/user/me"
},
{
"Status": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"doc_count": 3055,
"key": 200
}
]
},
"doc_count": 3055,
"key": "/user"
}
]
}
}
}
But I have few URL where user ID comes inbetween the URLs.
For eg URLs like
/account/me/3417375321062014.cust
DJXXODKNPA1581975RI/PlanDetails
KXVEIPBYSR1597110677RI/payment
Because of distinct user ID inside the URL, elastic search considers these as separate one.
User ID is a mix of alphabets and numbers, sometimes only numbers and few only alphabets.
I need to replace these userID's in URL and count them as one
Lets say there are two hits with 200 status for /payment
DJXXODKNPA1583201975RI/payment
KXVEIPBYSR1597110677RI/payment
I should be able to replace the user ID with * and count as one API so I get output like
*/payment 200:2
But now it considers these as distinct URL/API, because of this I am unable to get the correct Top 5 Hit urls/APIs.
Any idea on this would be a great help. Thanks

How to merge same key json data into one in c++ using nlhoman json

I have below JSON data:
{
"Created": "2019-08-01T14:36:49Z",
"Tags": [
{
"ObjectId": "1",
"Time": 6,
"TrackerId": "W1"
},
{
"ObjectId": "2",
"Time": 4,
"TrackerId": "E34"
},
{
"ObjectId": "3",
"Time": 4,
"TrackerId": "W1"
},
{
"ObjectId": "4",
"Time": 8,
"TrackerId": "E34"
}
],
"id": 0
}
In the above JSON data, we can see that we have 4 object id's but only 2 tracker id. I need to merge the data which has the same TrackerId and also add their time. So above data will become:
{
"Created": "2019-08-01T14:36:49Z",
"Tags": [
{
"Time": 10,
"TrackerId": "W1"
},
{
"Time": 12,
"TrackerId": "E34"
}
],
"id": 0
}
I am using Nlohmann JSON library for C++. How can we achieve this?
You can build a map of the trackers and then feed them into the JSON object:
json merge_objects(const json& data)
{
std::map<std::string, int> times;
for (const auto& entry : data["Tags"]) {
times[entry["TrackerId"]] += static_cast<int>(entry["Time"]);
}
json result;
result["Created"] = data["Created"];
for (const auto& [id, time] : times) {
json tag;
tag["Time"] = time;
tag["TrackerId"] = id;
result["Tags"].push_back(tag);
}
return result;
}
(live demo)

loopback 3 string not matched with and condition

i am trying to filter with multiple value in loopback 3. 'AND condition' is not working. Numeric value filter working good but string value not matched.
this is my condition is now :
{ "and": [{ "id": { "neq": "5d146f1f7a62651b8162fcf3" } },
{ "gender": "male" }, { "age": { "between": [16, 60] } },
{ "interestId": { "inq": ["5d11c0ce59e2b64e8dc12956", "5d11c0e259e2b64e8dc12958", "5d11c08659e2b64e8dc12953", "5d11c01759e2b64e8dc1294b", "5d11c03759e2b64e8dc1294d"] } },
{ "location": { "near": { "lat": 26.8574194, "lng": 75.7977288 }, "maxDistance": 6, "unit": "kilometers" } },
{ "maritalStatus": { "like": {} } }, { "college": { "like": {} } },
{ "career": { "like": {} } }, { "currentJob": { "like": {} } },
{ "height": { "between": [50, 89] } }, { "weight": { "between": [72, 192] } }, { "bodyBuild": "Slim" }] }
String value is not push in array.
if (interestId.highSchool && interestId.highSchool !="") {
var highschool = new RegExp('.*' + interestId.highSchool + '.*', "i");
query.and.push({
highSchool: { like: highschool }
})
}
where is issue i don't understand. if i did't pass string value it's working fine.
Try following way:-
if (interestId.highSchool && interestId.highSchool !="") {
query.and.push({
highSchool: {regexp: new RegExp('.*' + interestId.highSchool + '.*', "i")}
})
}

QStringList.size returning two values

In this section of my code I want to return the size of my QStringList
QStringList Attribute;
int tableSize = 0;
QString string= reply->readAll();
QRegExp expretion("[{}]");
Attribute = string.split(expretion);
tableSize = Attribute.size();
qDebug()<<tableSize;
qDebug()<<tableSize; This is returning to me two values (1 and 7)
Value of string
[
{
"idInstallation": 10,
"tstId": 1,
"knockPerTest": 10,
"description": "aaaaaaaaaaaaaaaaaaaa",
"state": "1",
"dateCreation": "2019-07-09T12:46:00",
"dateStart": "2019-07-09T12:46:00",
"datePause": "2019-07-09T12:46:00",
"dateStop": "2019-07-09T12:46:00"
},
{
"idInstallation": 10,
"tstId": 2,
"knockPerTest": 20,
"description": "bbbbbbbbbbbbbbbbbbb",
"state": "1",
"dateCreation": "2019-07-09T12:46:00",
"dateStart": "2019-07-09T12:46:00",
"datePause": "2019-07-09T12:46:00",
"dateStop": "2019-07-09T12:46:00"
},
{
"idInstallation": 10,
"tstId": 3,
"knockPerTest": 30,
"description": "ccccccccccccccccccccc",
"state": "1",
"dateCreation": "2019-07-09T12:46:00",
"dateStart": "2019-07-09T12:46:00",
"datePause": "2019-07-09T12:46:00",
"dateStop": "2019-07-09T12:46:00"
}
]
My goal using split is to separate the jason records I'm getting.
for example
Attribute = json.split(expretion);
I can't find the error
When I print to the console, it returns me the following values:

performing priority query in mongo

sample document :
{"name":"John", "age":35, "address":".....",.....}
Employees whose join_month=3 is priority 1
Employees whose address contains the string "Avenue" is priority 2
Employees whose address contains the string "Street" is priority 3
Employees whose address contains the string "Road" is priority 4
As of now, I'm at this stage:
db.collection.aggregate([
{ "$match": {
"$or": [
{ "join_month": 3 },
{ "address": /.*Avenue.*/i },
{ "address": /.*Street.*/i },
{ "address": /.*Road.*/i }
]
}},
{ "$project": {
"name": 1,
"age": 1,
"_id": 0,
"priority": { ?????????? }
}},
{ "$sort": { "priority": 1 } }
])
I'm stuck at priority field. What should I put there?
Using the aggregation framework you would "ideally" want to use the $regex filter within the $cond logical operator in the $project pipeline step but unfortunately MongoDB is yet to support this.
There is a JIRA ticket for this currently open $project filter using $regex
However, a workaround (though not the best solution performant-wise) would be to use map-reduce. Consider populating a test collection:
db.test.insert([
{ _id: 0, "join_month": 12, "address": "33 Point Avenue", "name": "John", "age":35 },
{ _id: 1, "join_month": 10, "address": "2A Broad Street, Surbub", "name": "Jane", "age":21 },
{ _id: 2, "join_month": 3, "address": "127 Umpstreeten Road, Surbub", "name": "Alan", "age":63 },
{ _id: 3, "join_month": 3, "address": "127 Umpstreeten Road, Surbub", "name": "Louise", "age":30 }
])
Define your map function as:
var mapper = function() {
var priority;
if (this.join_month==3){
priority = 1;
}
else if (this.address.match(/Avenue/i)){
priority = 2;
}
else if (this.address.match(/Street/i)){
priority = 3;
}
else if (this.address.match(/Road/i)){
priority = 4;
}
else {
priority = 99;
}
var value = {
"name": this.name,
"age": this.age,
"priority": priority
};
emit( this._id, value );
};
The reduce function follows:
var reducer = function() { };
Then run the mapduce operation on the test collection and store the result in the collection mr_result
db.test.mapReduce(mapper, reducer, {
"out": 'mr_result'
"query": {
"$or": [
{ "join_month": 3 },
{ "address": /.*Avenue.*/i },
{ "address": /.*Street.*/i },
{ "address": /.*Road.*/i }
]
}
})
Query the result collection:
db.mr_result.find().sort({ "priority": 1})
Sample Output
{ "_id" : 2, "value" : { "name" : "Alan", "age" : 63, "priority" : 1 } }
{ "_id" : 3, "value" : { "name" : "Louise", "age" : 30, "priority" : 1 } }
{ "_id" : 0, "value" : { "name" : "John", "age" : 35, "priority" : 2 } }
{ "_id" : 1, "value" : { "name" : "Jane", "age" : 21, "priority" : 3 } }