Ask a Question

how can read the account number in json response and store it in datasource/properties?

SOLVED
madhu_112
Occasional Contributor

how can read the account number in json response and store it in datasource/properties?

  1. I have an account number
  2. i have a JSON response that has 100 records with different account number and other details
  3. how do i find this account number exists in 100 records and store it somewhere like datasource or property?
1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: how can read the account number in json response and store it in datasource/properties?

Add script assertion for the REST test step.

 

 

/**
Save the 265954 at test case level custom property say ACCOUNT_PREFIX and it will read from there; next time if the value changes, you can replace there without modifying assertion
*/
assert context.response, 'The response is empty or null'
def accountPrefix = context.testCase.getPropertyValue('ACCOUNT_PREFIX')
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Retrive respective account number from the response
def accountNumber = json.find {it.value.startsWith(accountPrefix)}.value
log.info "Account number fetched: ${accountNumber}"
//Save account number at test case level
//Use can use ${#TestCase#ACCOUNT_NUMBER} In next test steps if you need it.
context.testCase.setPropertyValue('ACCOUNT_NUMBER', accountNumber)

 

Please  follow comments in above code snippet



Regards,
Rao.

View solution in original post

9 REPLIES 9
nmrao
Community Hero

Re: how can read the account number in json response and store it in datasource/properties?

Please show the json response.


Regards,
Rao.
madhu_112
Occasional Contributor

Re: how can read the account number in json response and store it in datasource/properties?

this is my account number 265954 which i got from one response

now in below response this (265954) account is there in third record ("value":"265954F3365220210621") first 6 digits is my account number 

 

[

{

"part": 1,

"set": 265,

"ifsc":"88977",

"value":"569954F3365220210621"

},

{

"part": 2,

"set": 266,

"ifsc":"88978",

"value":"965954F3365220210621"

},

{

"part": 3,

"set": 267,

"ifsc":"88979",

"value":"265954F3365220210621"

}

]

 

so since i have the account number in "value" field i want to copy the whole field value (265954F3365220210621) and store it in datasource or property..

nmrao
Community Hero

Re: how can read the account number in json response and store it in datasource/properties?

Add script assertion for the REST test step.

 

 

/**
Save the 265954 at test case level custom property say ACCOUNT_PREFIX and it will read from there; next time if the value changes, you can replace there without modifying assertion
*/
assert context.response, 'The response is empty or null'
def accountPrefix = context.testCase.getPropertyValue('ACCOUNT_PREFIX')
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Retrive respective account number from the response
def accountNumber = json.find {it.value.startsWith(accountPrefix)}.value
log.info "Account number fetched: ${accountNumber}"
//Save account number at test case level
//Use can use ${#TestCase#ACCOUNT_NUMBER} In next test steps if you need it.
context.testCase.setPropertyValue('ACCOUNT_NUMBER', accountNumber)

 

Please  follow comments in above code snippet



Regards,
Rao.

View solution in original post

madhu_112
Occasional Contributor

Re: how can read the account number in json response and store it in datasource/properties?

I'm getting error like this, from the logs i can see that from the response I'm getting only the account number but i want the entire string which has that account number

WhatsApp Image 2021-07-17 at 10.31.35 PM.jpeg

nmrao
Community Hero

Re: how can read the account number in json response and store it in datasource/properties?

Have you followed the comments provided?

Solution provided as per information provided. And it fetches entire account number.

You can try it online here
What is the value for ACCOUNT_PREFIX ?

What is the value for ACCOUNT_NUMBER after running the assertion ?
Do you have multiple values with same ACCOUNT_PREFIX in your response ?
Please show the screen shot for test case custom properties, script assertion and response.



Regards,
Rao.
madhu_112
Occasional Contributor

Re: how can read the account number in json response and store it in datasource/properties?

Sorry. i think I've posted the wrong response, 

[

{

"part": 1,

"set": 265,

"ifsc":"88977",

"value": "{\"event\":\"569954F3365220210621"}"

},

{

"part": 2,

"set": 266,

"ifsc":"88978",

"value":"{\"event\":\"965954F3365220210621"}"

},

{

"part": 3,

"set": 267,

"ifsc":"88979",

"value":"{\"event\":\"265954F3365220210621"}"

}

]

madhu_112
Occasional Contributor

Re: how can read the account number in json response and store it in datasource/properties?

There is one more thing the accountPrefix doesn't always starts at starting it will be in the middle or end as well

madhu_112
Occasional Contributor

Re: how can read the account number in json response and store it in datasource/properties?

Got the solution, I used contains instead of startsWith.. 🙂 

madhu_112
Occasional Contributor

Re: how can read the account number in json response and store it in datasource/properties?

But you what i'm getting only one record with the given account number there might be some other transactions with the same account number .. how can i get all the string that has this account number?

 

cancel
Showing results for 
Search instead for 
Did you mean: