cancel
Showing results for 
Search instead for 
Did you mean: 

Extract Json Node value based on sibling node value in Scripted assertion

Highlighted
New Contributor

Extract Json Node value based on sibling node value in Scripted assertion

I am having a JSON as below .I need to extract accountBalance based on accountNumber( value of which I need to take from a property ). I need to assert the balance with another value(which is sum of two properties value)  thus I am using scripted assetions

 

 

[
{
"accountNumber": 462210,
"accountType": "string",
"accountBalance": 1000,
"accountOpeningDate": "07-Jul-2019 23:42:06",
"cif": 379591
},
{
"accountNumber": 474555,
"accountType": "string",
"accountBalance": 1000,
"accountOpeningDate": "07-Jul-2019 23:46:23",
"cif": 379591
},
{
"accountNumber": 486900,
"accountType": "string",
"accountBalance": 1000,
"accountOpeningDate": "08-Jul-2019 00:00:15",
"cif": 335643
}
]

 

I using groovy.json.JsonSlurper().parseText and able to get array of balance. But I need to get balance based on specific accountNumber ( which I am getting through messageExchange.modelItem.testStep.testCase.getPropertyValue("Account") )

 

Kindly help me with extracting the balance based on accountNumber value.Please let me know if there is better approach to do this.

2 REPLIES 2
Highlighted
Community Leader

Re: Extract Json Node value based on sibling node value in Scripted assertion

Hi @ekta_sriv ,

 

Use HashMap in this case, it will help you to extract value and save unique values.

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

Re: Extract Json Node value based on sibling node value in Scripted assertion

def theAccount = new groovy.json.JsonSlurper()
    .parseText(context.response)
    .find { it.accountNumber == context.'${#TestCase#Account}' }

assert theAccount.accountBalance == context.'${#TestCase#Prop1}'.toBigDecimal() + 
context.'${#TestCase#Prop2}'.toBigDecimal()
New Here?
Join us and watch the welcome video:
Top Kudoed Authors