How to extract two value from json under one condition and randomize it in jmeter? - groovy

Below is the server response in a JSON format which I need to work on, In this JSON some of the objects I need to pass in the next request, which I am successfully able to do for the first occurrence, but the problem is coming on randomization in the below JSON
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sword of Honour",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "test_title",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Sword of Honour",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "India",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
if I apply $..book[0][?(#.title == 'Sword of Honour')] condition I am seeing successful output
test_1={"category":"reference","title":"Sword of Honour","author":"Nigel Rees","price":8.95}
test_2={"category":"fiction","title":"Sword of Honour","author":"Herman Melville","price":8.99,"isbn":"0-553-21311-3"}
if I apply $..book[0][?(#.title == 'Sword of Honour')].author condition I am seeing successful output
test_1=Nigel Rees
test_2=Herman Melville
But I want to extract both author's name and price for the book that has the title == 'Sword of Honour'
Actual output = able to retrieve one value with the condition $..book[0][?(#.title == 'Sword of Honour')].author
Expected output = I want to receive two values from the same list randomly. and it should look like this
Iteration 1-
test_1={"author":"Nigel Rees","price":8.95}
Iteration 2-
test_2={"author":"Herman Melville","price":8.99,}
Iteration 3-
test_2={"author":"Herman Melville","price":8.99,}
I had tried with the V function as well but got only one value. (book{__V([?(#.title == 'Sword of Honour')]_${__Random(1,${title_matchNr},)},)})

Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def swords = new groovy.json.JsonSlurper().parse(prev.getResponseData()).store.book.findAll { book -> book.title == 'Sword of Honour' }
swords.eachWithIndex { def sword, int index ->
def payload = [:]
payload.put('author', sword.author)
payload.put('price', sword.price)
vars.put('test_' + (index + 1), new groovy.json.JsonBuilder(payload).toPrettyString())
}
vars.put('test_matchNr', swords.size() as String)
Now you should be able to refer random author/price pair using __V() and __Random() functions combination like: ${__V(test_${__Random(1,${test_matchNr},)},)}
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy: What Is Groovy Used For?

Related

Power Automate FIlter Array with Array Object as Attribute

i have a Object-Array1 with some Attributes that are Object-Array2. I want to filter my Object-Array1 only to these elements, that contain a special value in Object-Array2. How wo i do this? Example:
{
"value": [
{
"title": "aaa",
"ID": 1,
"Responsible": [
{
"EMail": "abc#def.de",
"Id": 1756,
},
{
"EMail: "xyz#xyz.com",
"Id": 289,
}
]
},
{
"title": "bbbb",
"ID": 2,
"Responsible": [
{
"EMail": "tzu#iop.de",
"Id": 1756,
}
]
}
]
}
I want to filter my Object-Array1 (with title & id) only to these elements, that contain abc#def.de
How do i do this in Power Automate with the "Filter Array" Object? I tried this way, but didn't work:
Firstly, you haven't entered an expression, you've entered text. That will never work.
Secondly, even if you did set that as an expression, I don't think you'll be able to make it work over an array, at least, not without specifying more properties and making it a little more complex.
I think the easiest way is to use a contains statement after turning the item into a string ...
The expression I am using on the left hand side is ...
string(item()?['Responsible'])
... and this is the result ...

Jmeter how to construct payload with same size from csv file by iteration

I had a csv file that contains 10 user ids and the requirement is to construct a payload for every 5 users in the csv. Below is my code:
def start = (vars.get('__jm__Thread Group__idx') as int)
def offset = 5
def payload = [:]
def data = []
def file = 'C:/path/dataset.csv'
start.upto(offset, { index ->
def lineFromCsv = new File(file).readLines().get(index)
data.add(['userId': lineFromCsv.split(',')[0], 'groupId': lineFromCsv.split(',')[1]])
})
payload.put('data', data)
log.info("%%%The Payload is%%%:" + payload)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
My 1st question is why there were 6 items in the first payload (1st iteration), where I was expecting 5. And there were 5 items in the 2nd payload (2nd iteration) as expected. Every payload was supposed to have the same # of items in it.
My 2nd question is that how do I make the 2nd payload start parsing from where the 1st payload left off. The 2nd payload was supposed to contain the next 5 users in the csv? There should not have any overlap items between each payloads.
Below is the payload:
1st payload:
POST data:
{
"data": [
{
"userId": "fakeUser3k0000002",
"groupId": "1"
},
{
"userId": "fakeUser3k0000003",
"groupId": "2"
},
{
"userId": "fakeUser3k0000004",
"groupId": "2"
},
{
"userId": "fakeUser3k0000005",
"groupId": "3"
},
{
"userId": "fakeUser3k0000006",
"groupId": "4"
},
{
"userId": "fakeUser3k0000007",
"groupId": "5"
}
]
}
2nd payload:
POST data:
{
"data": [
{
"userId": "fakeUser3k0000003",
"groupId": "2"
},
{
"userId": "fakeUser3k0000004",
"groupId": "2"
},
{
"userId": "fakeUser3k0000005",
"groupId": "3"
},
{
"userId": "fakeUser3k0000006",
"groupId": "4"
},
{
"userId": "fakeUser3k0000007",
"groupId": "5"
}
]
}
def start = (vars.get('__jm__Thread Group__idx') as int)
def offset = 5
i guess Thread Group is your jmeter loop name.
you have to build loop like this
(start*offset).step((start+1)*offset,1){ index->
println it
}
so for start=5 you'll have index 25 to 29
In order to get 5 items in first payload you need to define the offset to 4 as __jm__Thread Group__idx variable value is 0 during the first iteration, you can check it using Debug Sampler and View Results Tree listener combination
In order start 2nd iteration from position 4 you need to store the offset value into a JMeter Variable after constructing the first payload and read it during 2nd iteration.

Unable to fetch the entire column index based on the value using JSONPath finder in npm

I have the below response payload and I just want to check the amount == 1000 if it's matching then I just want to get the entire column as output.
Sample Input:
{
"sqlQuery": "select SET_UNIQUE, amt as AMOUNT from transactionTable where SET_USER_ID=11651 ",
"message": "2 rows selected",
"row": [
{
"column": [
{
"value": "22621264",
"name": "SET_UNIQUE"
},
{
"value": "1000",
"name": "AMOUNT"
}
]
},
{
"column": [
{
"value": "226064213",
"name": "SET_UNIQUE"
},
{
"value": "916",
"name": "AMOUNT"
}
]
}
]
}
Expected Output:
"column": [
{
"value": "22621264",
"name": "SET_UNIQUE"
},
{
"value": "1000",
"name": "AMOUNT"
}
]
The above sample I just want to fetch the entire column if the AMOUNT value will be 1000.
I just tried below to achieve this but no luck.
1. row[*].column[?(#.value==1000)].column
2. row[*].column[?(#.value==1000)]
I don't want to do this by using index. Because It will be change.
Any ideas please?
I think you'd need nested expressions, which isn't something that's widely supported. Something like
$.row[?(#.column[?(#.value==1000)])]
The inner expression returns matches for value==1000, then the outer expression checks for existence of those matches.
Another alternative that might work is
$.row[?(#.column[*].value==1000)]
but this assumes some implicit type conversions that may or may not be supported.

Access a dictionary value based on the list of keys

I have a nested dictionary with keys and values as shown below.
j = {
"app": {
"id": 0,
"status": "valid",
"Garden": {
"Flowers":
{
"id": "1",
"state": "fresh"
},
"Soil":
{
"id": "2",
"state": "stale"
}
},
"BackYard":
{
"Grass":
{
"id": "3",
"state": "dry"
},
"Soil":
{
"id": "4",
"state": "stale"
}
}
}
}
Currently, I have a python method which returns me the route based on keys to get to a 'value'. For example, if I want to access the "1" value, the python method will return me a list of string with the route of the keys to get to "1". Thus it would return me, ["app","Garden", "Flowers"]
I am designing a service using flask and I want to be able to return a json output such as the following based on the route of the keys. Thus, I would return an output such as below.
{
"id": "1",
"state": "fresh"
}
The Problem:
I am unsure on how to output the result as shown above as I will need to parse the dictionary "j" in order to build it?
I tried something as the following.
def build_dictionary(key_chain):
d_temp = list(d.keys())[0]
...unsure on how to
#Here key_chain contains the ["app","Garden", "Flowers"] sent to from the method which parses the dictionary to store the key route to the value, in this case "1".
Can someone please help me to build the dictionary which I would send to the jsonify method. Any help would be appreciated.
Hope this is what you are asking:
def build_dictionary(key_chain, j):
for k in key_chain:
j = j.get(k)
return j
kchain = ["app","Garden", "Flowers"]
>>> build_dictionary(kchain, j)
{'id': '1', 'state': 'fresh'}

Type mismatch when importing JSON into Neo4J with Py2Neo

I have a JSON file, the head of which looks like this:
"exports": {
"type": "WordsAndPhrases",
"date": "2018-08-02T10:07:58.047669Z",
"relevantYears": "2012,2013,2014,2015,2016,2017",
"Words": {
"H1": "WORDS AND PHRASES:",
"Word": [
{
"Phrase": {
"id": "phrase_2011001932",
"title": "A common"
},
"Document": "Law of Property Act 1925, s 193(1) (as amended)",
"Refs": {
"CaseTitle": {
"id": "2011201246",
"title": "ADM Milling Ltd v Tewkesbury Town Council"
},
"title": "None",
"citations": "Lewison J [2011] EWHC 595 (Ch); [2012] Ch 99; [2011] 3 WLR 674, Ch D"
}
},
I am using the following script to import the JSON data into Neo4J:
import json
from py2neo import Graph, authenticate
authenticate("localhost:7474", "neo4j", "foobar")
graph = Graph()
with open('wp.json') as data_file:
json = json.load(data_file)
query = """
WITH {json} AS document
UNWIND document.exports.Words.Word AS Word
MERGE (Phrase:a {phrase: Word.Phrase.title})
MERGE (Document:b {document: Word.Document})
FOREACH (case in Word.Refs.CaseTitle.title | MERGE (Report:z {report: case}))
"""
# Send Cypher query.
print (graph.run(query, json = json).dump())
The first two MERGE queries work fine. However, the FOREACH query is proving to be problematic. I'm using the FOREACH query to deal with instances where there are multiple CaseTitle properties in a single block, for example:
{
"Phrase": {
"id": "phrase_2011002042",
"title": "Acts contrary to purposes and principles of United Nations"
},
"Document": "Council Directive 2004/83/EC, art 12(2)(c)",
"Refs": [
{
"CaseTitle": {
"id": "2011201814",
"title": "Federal Republic of Germany v B"
},
"title": "None",
"citations": "(Joined Cases C-57/09 and C-101/09); [2012] 1 WLR 1076, ECJ"
},
{
"CaseTitle": {
"id": "2016008987",
"title": "Commissaire général aux réfugiés et aux apatrides v Lounani"
},
"title": "None",
"citations": "EU:C:2017:71; [2017] 4 WLR 52, ECJ"
}
]
},
When I run the script, the following error occurs:
py2neo.database.status.CypherTypeError: Type mismatch: expected a map but was List{Map{title -> String("None"), CaseTitle -> Map{title -> String("Federal Republic of Germany v B"), id -> String("2011201814")}, citations -> String("(Joined Cases C-57/09 and C-101/09); [2012] 1 WLR 1076, ECJ")}, Map{title -> String("None"), CaseTitle -> Map{title -> String("Commissaire général aux réfugiés et aux apatrides v Lounani"), id -> String("2016008987")}, citations -> String("EU:C:2017:71; [2017] 4 WLR 52, ECJ")}}
The JSON appears to be valid. Can anyone recommend a way of dealing with this error?
Instead of Word.Refs.CaseTitle.title you should use Word.Refs[0].CaseTitle.title. In your JSON it is clear that refs is an array, and you're treating it like an object. The error message is saying this -- when it tries to dereference "CaseTitle" under "Refs", it is expecting a map, but what you gave it was a list of maps.

Resources