Forum Discussion

madhu_112's avatar
madhu_112
Occasional Contributor
3 years ago
Solved

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?
  • nmrao's avatar
    nmrao
    3 years ago

    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

9 Replies

    • madhu_112's avatar
      madhu_112
      Occasional Contributor

      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's avatar
        nmrao
        Champion Level 3

        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