Forum Discussion

ekta_sriv's avatar
ekta_sriv
New Contributor
6 years ago

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.

  • JHunt's avatar
    JHunt
    Community Hero
    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()