Gatling : regex to get a specific value from a JSON - regex

I have this JSON Object coming from an HTML.
{
"isCompany": false,
"accommodations": [
{
"id": "00000000031000006435",
"isChecked": false,
"name": "Théo",
"addressLine1": "Rue des patriotes 40 Y",
"addressLine2": "1050 Ixelles",
"nightsDeclared": 5,
"schoolNightsDeclared": 3,
"schoolNightsAttached": 0,
"taxableNights": 2.0,
"totalPayment": 0.0,
"isInProgress": true,
"isLate": false,
"isPayed": "false",
"deadline": "2021-12-31",
"initialAmount": 0.0,
"remainingAmount": 0.0
},
{
"id": "00000000031000006438",
"isChecked": false,
"name": "Théo",
"addressLine1": "Rue des Gens 45 B",
"addressLine2": "1040 Etterbeek",
"nightsDeclared": 5,
"schoolNightsDeclared": 3,
"schoolNightsAttached": 0,
"taxableNights": 2.0,
"totalPayment": 0.0,
"isInProgress": true,
"isLate": false,
"isPayed": "false",
"deadline": "2021-12-31",
"initialAmount": 0.0,
"remainingAmount": 0.0
}
]
}
I know that in Gatling, it is possible to get the accommodation id by writing this regex :
check(regex(""""accommodations":\[\{"id":"(.*?)"""").saveAs("accommodationId"))
Now my question is, what is the regex that gets the "isInProgress"?

Don't!
Using regex in this specific case could result in your code breaking on slight input changes and will result in unreadable code.
Instead deserialize and access as a dictionary?
[a['id'] for a in json.loads(json_string)['accommodations']]
Also, have you tried to simply replace id with the name of the field you want?
If you insist on using regex for this, check out online regex sites like regex101.com, regexr.com, regextester.com etc. (search for "online regex test") and try to solve this yourself. If your code does not work, ask a question again.

Ok, you have this in your regex
"id":"(.*?)"
You need just change to expected key name as isInProgress or any another. Also pay attention on " around (.*?) - since the value for id a string, they are needed, but value in isInProgress with another type.

Related

How to use match as with Regular Expression in Mongodb with in Aggregate switch case?

Here what i did.
Inside $AddFields
{
ClientStatus:{
$switch: {
branches: [
{ case: {
$eq:
[
"$CaseClientStatus",
/In Progress/i
]},
then:'In Progress'
},
{ case: {
$eq:
[
"$CaseClientStatus",
{regex:/Cancelled/i}
],
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
}
but with this my ClientStatus is showing only Complete,Other,Case On Hold not the one with specified with regex. alghough field contains those words.
here is the one of the doc
{
"CandidateName": "Bruce Consumer",
"_id": "61b30daeaa237672bb7a17cc",
"CaseClientStatus": "Background Check Case In Progress",
"TAT": "N/A",
"CaseCloseDate": null,
"FormationAutomationStatus": "Automated",
"MethodOfDataSupply": "Automated",
"Status": "Background Case In Progress",
"CreatedDate": "2021-12-10T08:19:58.389Z",
"OrderId": "Ord3954",
"PONumber": "",
"Position": "",
"FacilityCode": "",
"IsCaseClose": false,
"Requester": "Shah Shah",
"ReportErrorList": 0
}
Assuming you are on version 4.2 or higher (and you should be because 4.2 came out almost 2 years ago) then the $regexFind function gives you what you need. Prior to 4.2, regex was only available in a $match operator, not in complex agg expressions. Your attempt above is admirable but the // regex syntax is not doing what you think it should be doing. Notably, {regex:/Cancelled/i} is simply creating a new object with key regex and string value /Cancelled/i (including the slashes) which clearly will not equal anything in $CaseClientStatus. Here is a solution:
ClientStatus:{
$switch: {
branches: [
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /In Progress/i}}]
}, then:'In Progress'},
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /Cancelled/i}}]
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
It looks like you are trying to take a somewhat free-form status "description" field and create a strong enumerated status from it. I would recommend that your $ClientStatus output be more code-ish e.g. IN_PROGRESS, COMPLETE, CXL etc. Eliminate case and certainly whitespace.

CARBONE.IO Conditional display (ifEQ)

I'm using Carbone to generate PDF with variables.
I need to make a conditional display. If a product have a specific category I need to display an information.
I tried to make the condition with ifEQ, show and showBeggin // showEnd but it does'nt work.
// My JSON
const json = {
shopping : {
sales :[
{
product_name: "Apple",
product_price: 2,
product_category: "fruits",
comment: "",
},
{
product_name: "Coke",
product_price: 3,
product_category: "soda",
comment: "",
},
{
product_name: "Cucumber",
product_price: 1.5,
product_category: "vegetable",
comment: "",
},
{
product_name: "Vodka",
product_price: 15,
product_category: "Alcohol",
comment: "Dangerous for health, prohibited at least 18 years old",
},
]
}
}
What I made on my document :
State
{d.shopping.sales[i].product_name} {d.shopping.sales[i].product_category(“Alcohol”):showBeggin} Information {d.shopping.sales[i].comment}, {d.shopping.sales[i].product_price}, {d.shopping.sales[i].produt_name}{d.shopping.sales[i].product_category::showEnd}
{d.shopping.sales[i+1].product_name}
Thank's for help !
Your example got a couple of errors, some fixes to do:
it is missing the ifEQ just before the showBegin
if the ifEQ argument is a string, it must be wrap with single quotes
showBeggin should be written showBegin
the last showEnd should be preceded by only one colon.
Here is your template with all corrections:
State
{d.shopping.sales[i].product_name} {d.shopping.sales[i].product_category:ifEQ('Alcohol'):showBegin}
Information {d.shopping.sales[i].comment}, {d.shopping.sales[i].product_price}, {d.shopping.sales[i].produt_name}{d.shopping.sales[i].product_category:showEnd}
{d.shopping.sales[i+1].product_name}
The conditional block documentation has been improved a lot, to get more examples: https://carbone.io/documentation.html#conditioned-output
Have a great day!

Match decimal as string to 0 in MongoDB without regex

I have a MongoDB collection where decimal numbers are stored as string. I need to find all those items that have one of such fields, quantity, equal to 0. Thus when looking for 0 I am actually looking for the strings:
"0"
"0.0"
"0.00"
...
and so on
I tried to use $toInt as follows
db.MyCollection.find({$toInt(Product.Quantity): 0})
but the query is flagged as wrong, it does not even get executed
After some digging I finally found the solution using regex:
db.MyCollection.find({"Product.Quantity": {$regex: "^0+$|^0+(\.0+)"}})
which indeed works but it I am sure that there is a more straightforward solution, it cannot be so utterly complex. Does anybody have a better solution?
Does this help?
Live
db.collection.aggregate({
$match: {
$expr: {
$eq: [
{
"$convert": {
"input": "$key",
"to": "double",
"onError": "$key",
"onNull": "$key"
}
},
0
]
}
}
})
Just replace key by your field.
On this example, you can see how it is operating under the hood.
Or using find
db.collection.find({
$expr: {
$eq: [
{
"$convert": {
"input": "$key",
"to": "double",
"onError": "$key",
"onNull": "$key"
}
},
0
]
}
})
Yes, regex is convenient to search for numbers in string format. You could simplify the regex a bit:
db.MyCollection.find({"Product.Quantity": {$regex: "^0(\.0+)?$"}})
Explanation of regex:
^ ... $ - anchor at the beginning and end
0 - expect a 0
(\.0+)? - followed by optional .0, .00, etc

Get keys from Json with regex Jmeter

I'm hustling with regex, and trying to get the id's from this body.
But only the id´s in the members list, and not the id in the verified key. :)
To clarify, I'm using Regular Expression Extractor in JMeter
{
"id": "9c40ffca-0f1a-4f93-b068-1f6332707d02", //<--not this
"me": {
"id": "38a2b866-c8a9-424f-a5d4-93b379f080ce", //<--not this
"isMe": true,
"user": {
"verified": {
"id": "257e30f4-d001-47b3-9e7f-5772e591970b" //<--not this
}
}
},
"members": [
{
"id": "88a2b866-c8a9-424f-a5d4-93b379f780ce", //<--this
"isMe": true,
"user": {
"verified": {
"id": "223e30f4-d001-47b3-9e7f-5772e781970b" //<--not this
}
}
},
{
"id": "53cdc218-4784-4e55-a784-72e6a3ffa9bc", //<--this
"isMe": false,
"user": {
"unverified": {
"verification": "XYZ"
}
}
}
]
}
at the moment I have this regex :
("id": )("[\w-]+")
But as you can see here it returns every guid
Any ideas on how to go on?
Thanks in advance.
Since the input data type is JSON, it is recommended to use the JMeter's JSON Path Extractor Plugin.
Once you add it, use the
$.members[*].id
JSON path expression to match all id values of each members in the document that are the top nodes.
If you may have nested memebers, you may get them all using
$..members[*].id
You may test these expressions at https://jsonpath.com/, see a test:

autofilling a dict python 2.x

I'm quite new to Python and programming in general, so apologies if this is quite basic or has been asked and answered before. Here is a sample of the data I'm working with:
{
"homeTeam": {
"formation": [
"4",
"4",
"2"
],
"lineupsSorted": [
{
"player": {
"name": "Scott P. Brown",
"slug": "scott-p-brown",
"shortName": "S. P. Brown",
"id": 19889,
"hasImage": true
},
"position": 1,
"shirtNumber": 1,
"substitute": false,
"positionName": "Goalkeeper",
"positionNameshort": "G",
"captain": false
},
{
"player": {
"name": "Carl Winchester",
"slug": "carl-winchester",
"shortName": "C. Winchester",
"id": 110785,
"hasImage": true
},
"position": 2,
"shirtNumber": 27,
"substitute": false,
"positionName": "Midfielder",
"positionNameshort": "M",
"captain": false
},
I am looking to automate populating defined names as I have done manually here:
hometeamPositions =['Goalkeeper','Midfielder','Defender','Defender','Defender','Midfielder','Midfielder','Midfielder','Midfielder','Forward','Forward','Goalkeeper','Defender','Defender','Midfielder','Midfielder','Forward','Forward']
hometeamPlayers = ['S. P. Brown','C. Winchester','M. Onariase','W.
Boyle','J. Cranston','H. Pell','J. Rowe','K. Storer','B. Waters','D.
Wright','D. Holman','R. Lovett','J. Barthram','T. Plavotic','J.
Munns','L. Davis','K. Wootton','J. Dayton']
As I will be repeating this process many hundreds of times with different data (same structure) I was wondering if anyone could give me some tips on automatically building these ranges?
Thanks,
Peter
I'm not sure I understood what is the problem you are trying to solve but I'll try to help.
Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:
hometeamPlayers = []
hometeamPositions = []
for player_dict in teams_dict['homeTeam']['lineupsSorted']:
hometeamPlayers.append(player_dict['player']['shortName'])
hometeamPositions.append(player_dict['positionName'])
The output on your example will be:
hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']