Need to Extract multi value key pair using regex - json

In My Web test project one request return below json in body tag. And I wants to extract all TimeEntryIds of Each FieldItem. using single RegEx extractor (Post porcessors) because I need to pass each TimeEntryid in successive web request.
{
"InvoiceItemId": 0,
"JobId": 9999,
"CreatedDate": "0001-01-01T00:00:00Z",
"LastUpdatedDate": "0001-01-01T00:00:00Z",
"FieldItem": [
{
"root": false,
"TimeEntryId": 1,
"UpdatedDate": "2019-07-19T13:14:29.823Z"
},
{
"root": false,
"TimeEntryId": 2,
"UpdatedDate": "2019-07-19T13:14:29.823Z"
},
{
"root": false,
"TimeEntryId": 3,
"UpdatedDate": "2019-07-19T13:14:29.823Z"
},
{
"root": false,
"TimeEntryId": 4,
"UpdatedDate": "2019-07-19T13:14:29.823Z"
}
]
}
I tried "TimeEntryId":(.\d+) regex to get the value but as per GUI option we get one key\value pair only in my case it is TimeEntryId:1.
Is there any short cut to get the all Key\Value pare in single RegEx extractor.

Use below regex with Jmeter JSON Extractor post processor
$.FieldItem.[*].TimeEntryId

JS solution
If you JSON.stringify() your data and then use data.match(regex); you will get back the array with all ids.

Related

Sort by nested fields in json in Power Automate

I'm trying to sort a JSON String in Power Automate by a nested field called "orderHint".
My JSON String looks like this:
[
{
"id": "5134",
"value": {
"isChecked": false,
"title": "This is another test",
"orderHint": "8585298133570680672PF"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
},
{
"id": "26576",
"value": {
"isChecked": true,
"title": "This is a test",
"orderHint": "8585498133570680672DE"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
}
]
When I'm trying to sort by "orderHint", I get an error:
"'The template language function 'sort' did not find the named sortField 'orderHint' on one or more objects in the array."
I'm using the following expression:
sort(variables('varArrayChecked'), 'value/orderHint')
Sorting by other fields works fine, e.g.:
sort(variables('varArrayChecked'), 'id')
Is there any way how I can sort by a nested field in a JSON String?
Thanks in advance!
You can use the Advanced Data Operations connector as it will do it for you in a single step.
The Flatten Object Array step is perfect for the payload you've provided.
You can see that it will take the data, flatten it and you have the ability to sort it on the way out (noting that the Array variable contains the exact JSON you provided in your question) ...
Note: Balance Output must be set to true in order for the sorting to occur.
Result
This is the resulting JSON order by orderHint ascending.
[
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
},
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
}
]
... and to show it in descending order (which is obvious, but simply change the sort order object value from Asc to Desc) ...
[
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
},
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
}
]

Regex to extract UUID without key value in json

I have below json from which I wants to extract '07199fca-b43f-4e58-b0fc-c1e254f34ac0' values. I got with multiple regex available in regex101 but every time I get all UUID value any way by which I get Unique value that has no json key assign.
JSON Syntax:
[
[
{
"clientId": 178,
"uniqueId": "5f7f919f-7e0f-4a1e-9a91-89b673896da6",
"displayName": "Automation Client Test",
"productVersion": "7.9.0.0"
},
{
"clientId": 1206,
"uniqueId": "096b3549-6899-4621-854c-e682aeb543bd",
"displayName": "TestClient1",
"productVersion": "7.9.0.0"
},
{
"clientId": 1356,
"uniqueId": "faad0e20-dd29-4146-8a8f-37648749aa4e",
"displayName": "Client Automation",
"productVersion": "7.9.0.0"
}
],
"07199fca-b43f-4e58-b0fc-c1e254f34ac0"
]
From your question it is not 100% clear what you are looking for, but if you are looking for the last element of the list that also satisfies [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} regex (with quotes around it), then:
(?:['"])([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})(?:['"]\s*]\s*$)
See Regex Demo
(?:['"]) Matches ' or ".
([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}) Your regex captured in capture group 1.
(?:['"]\s*]\s*$) Matches ' or " followed by 0 or more white space characters followed by a ] followed by 0 or more white space characters followed by the end of string.
The code:
import re
s = """ [
[
{
"clientId": 178,
"uniqueId": "5f7f919f-7e0f-4a1e-9a91-89b673896da6",
"displayName": "Automation Client Test",
"productVersion": "7.9.0.0"
},
{
"clientId": 1206,
"uniqueId": "096b3549-6899-4621-854c-e682aeb543bd",
"displayName": "TestClient1",
"productVersion": "7.9.0.0"
},
{
"clientId": 1356,
"uniqueId": "faad0e20-dd29-4146-8a8f-37648749aa4e",
"displayName": "Client Automation",
"productVersion": "7.9.0.0"
}
],
"07199fca-b43f-4e58-b0fc-c1e254f34ac0"
]"""
m = re.search(r"""(?:['"])([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})(?:['"]\s*]\s*$)""", s, flags=re.DOTALL|re.IGNORECASE)
if m:
print(m[1])
Prints:
07199fca-b43f-4e58-b0fc-c1e254f34ac0
JSON is a well-structured data, using regular expressions for parsing JSON is not the best idea, I would rather recommend going for JSON Extractor instead
The relevant JsonPath query would be as simple as $[1]
Demo:
More information: API Testing With JMeter and the JSON Extractor

jmeter - How to format json in body data (extracted from regular expression)

I'm trying strong text to extract json with regular extraction then post it in the next request body data with formatted json. For the json that I extracted, they are not formatted and I'm just wondering if there is any function or way to format it?
Get request with regular expression extractor (extracted the bold section)
{
"groupedData": [{
"key": "FirstItem",
"count": 1,
"groupID": 1,
"items": [{
**"keyID": 97215,
"film": {
"name": xxxx,
"id": xxx,
"vendorID": 0,
"type": "PG",
"xxxx": xxx
},
"subGroups": null**
}],
"totalRows": 1
}]
}
Post in the next request with extracted data (JSON data extracted from above request with regular expression is appearing as whole string and just wondering how can i format in this body data?)
{
"keyID": 123,
"name": "SYSGEN",
"period": {
"keyID": 427,
},
"periodID": 427,
"items": [{
**${JSON}**
}],
"group": 0,
"selRow": false,
"rowId": 1,
"$rowState": {
"invalid": false,
},
"XXXX": XXXX,
}],
"ZZZZZZ": "ZZZZZ"
}
You can format the JSON using __groovy() function, i.e.
If you have a JMeter Variable foo where the extracted JSON data is stored and refer to it as ${foo} in the HTTP Request
Replace your ${foo} variable reference with the following function:
${__groovy(groovy.json.JsonOutput.prettyPrint(vars.get('foo')),)}
That's it, the above Groovy expression will format the JSON which lives in ${foo} JMeter Variable
If you want to get response text between given boundaries use Boundary Extractor:
Left Boundary:
"items": [{
Right Boundary:
}],
You can also test it using View Results Tree
The Boundary Extractor Tester only works for text responses. It shows the plain text in the upper panel. The "Test" button allows the user to apply the Boundary Extractor query to the upper panel and the results will be displayed in the lower panel.

rename invalid keys from JSON

I have following flow in NIFI , JSON has (1000+) objects in it.
invokeHTTP->SPLIT JSON->putMongo
Flow works fine, till I receive some keys in json with "." in the name. e.g. "spark.databricks.acl.dfAclsEnabled".
my current solution is not optimal, I have jotted down bad keys, and using multiple replace text processor to replace "." with "_". I am not using REGEX, I am using string literal find/replace. So each time I am getting failure in putMongo processor, I am inserting new replaceText processor.
This is not maintainable. I am wondering if I can use JOLT for this? couple of info regarding input JSON.
1) no set structure, only thing that is confirmed is. everything will be in events array. But event object itself is free form.
2) maximum list size = 1000.
3) 3rd party JSON, so I cant ask for change in format.
Also, key with ".", can appear anywhere. So I am looking for JOLT spec that can cleanse at all level and then rename it.
{
"events": [
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1531896847915,
"type": "EDITED",
"details": {
"previous_attributes": {
"cluster_name": "Kylo",
"spark_version": "4.1.x-scala2.11",
"spark_conf": {
"spark.databricks.acl.dfAclsEnabled": "true",
"spark.databricks.repl.allowedLanguages": "python,sql"
},
"node_type_id": "Standard_DS3_v2",
"driver_node_type_id": "Standard_DS3_v2",
"autotermination_minutes": 10,
"enable_elastic_disk": true,
"cluster_source": "UI"
},
"attributes": {
"cluster_name": "Kylo",
"spark_version": "4.1.x-scala2.11",
"node_type_id": "Standard_DS3_v2",
"driver_node_type_id": "Standard_DS3_v2",
"autotermination_minutes": 10,
"enable_elastic_disk": true,
"cluster_source": "UI"
},
"previous_cluster_size": {
"autoscale": {
"min_workers": 1,
"max_workers": 8
}
},
"cluster_size": {
"autoscale": {
"min_workers": 1,
"max_workers": 8
}
},
"user": ""
}
},
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1535540053785,
"type": "TERMINATING",
"details": {
"reason": {
"code": "INACTIVITY",
"parameters": {
"inactivity_duration_min": "15"
}
}
}
},
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1535537117300,
"type": "EXPANDED_DISK",
"details": {
"previous_disk_size": 29454626816,
"disk_size": 136828809216,
"free_space": 17151311872,
"instance_id": "6cea5c332af94d7f85aff23e5d8cea37"
}
}
]
}
I created a template using ReplaceText and RouteOnContent to perform this task. The loop is required because the regex only replaces the first . in the JSON key on each pass. You might be able to refine this to perform all substitutions in a single pass, but after fuzzing the regex with the look-ahead and look-behind groups for a few minutes, re-routing was faster. I verified this works with the JSON you provided, and also JSON with the keys and values on different lines (: on either):
...
"spark_conf": {
"spark.databricks.acl.dfAclsEnabled":
"true",
"spark.databricks.repl.allowedLanguages"
: "python,sql"
},
...
You could also use an ExecuteScript processor with Groovy to ingest the JSON, quickly filter all JSON keys that contain ., perform a collect operation to do the replacement, and re-insert the keys in the JSON data if you want a single processor to do this in a single pass.

Parsing Different Json Objects in WP8

Please help me in parsing this Json sample as I'm not able to parse it because of the complexity of it as well as different objects inside it. I'm able to parse Json when a list of same objects & same structure but not like the one below.
[
{
"notificationBrowserHead":
{
"notificationId": 4,
"notificationType": "NEW_PRODUCT",
"creationTime": 1421933381000,
"notificationNormalUserId": 4,
"notificationViewed": false
},
"brandIdAndNameHolder":
{
"brandId": 1,
"name": "B1"
},
"brandLogo": null,
"productIdAndNameHolder":
{
"productId": 1,
"name": "JK product1"
}
},
{
"notificationBrowserHead":
{
"notificationId": 2,
"notificationType": "USER_INT_COMMENT",
"creationTime": 1421924403000,
"notificationNormalUserId": 2,
"notificationViewed": false
},
"uploadId": 22,
"uploadThumbnail": "/mediaUrl/location/thumbNail",
"uploadDescription": "upload 1 location desc",
"notificationCreator":
{
"normalUserId": 90,
"displayName": "amit"
},
"uploadRemoved": false
},
{
"notificationBrowserHead":
{
"notificationId": 1,
"notificationType": "NEW_LOCATION_VOTE",
"creationTime": 1421924403000,
"notificationNormalUserId": 1,
"notificationViewed": false
},
"locationIdAndNameHolder":
{
"locationId": 11,
"name": "Current King JK"
},
"locationLogo": null
}
]
Any help would be truly appreciated.
I presume that you receive different set of json properties when your NotificationType varies.
Solution 1:
Define all your members(the collection of all your properties that you receive for different types of notification) in a Class and use it for DeSerialization, so that the unwanted properties for your particular notification type will be null.
Solution 2:
Parser manually. Newtonsoft json documentation here
Make class "Notifications (or something)" and put inside everything you got back from json2csharp.com site, then use this framework http://www.newtonsoft.com/json to deserialize data as you download it from server and you should be able to get notificationType by Object.Notificationbrowserhead[x].notificationType or similar.