1

I am trying to print a response based on the particular parameters. For that, I have the response from an API as below:

{
    "ABC": {
        "code": "ABC",
        "isActive": "true",
        "lastUpdatedBy": "username",
        "execution": {
            "status": "0",     
        },
        "priority": "1"
    },
    "DEF": {
        "code": "DEF",
        "isActive": "true",
        "lastUpdatedBy": "username",
        "execution": {
            "status": "1",     
        },
        "priority": "1"
    },
    "GHI": {
        "code": "GHI",
        "isActive": "true",
        "lastUpdatedBy": "username",
        "execution": {
            "status": "2",     
        },
        "priority": "1"
    },
    "JKL": {
        "code": "JKL",
        "isActive": "true",
        "lastUpdatedBy": "username",
        "execution": {
            "status": "0",     
        },
        "priority": "1"
    },
}

Here is the feature file that I am using to print that particular value:

Feature: Value extraction
    Background:
    * def all = [ABC,DEF,GHI,JKL]

Scenario: Extract response value

Given url 
When method get
Then status 200
And def value = []
And eval for(var i=0;i<all.length;i++) {value.add(response['#(all[i])']["execution"]["status"]) }
And print value

I want to extract the value of all parameters whose status is "0".

print response["ABC"]["execution"]["status"]

The above line gives the result but I want to parameterise the ["ABC"] part Any help on this? Is there any other way I can achieve this or am I doing something wrong to achieve this particular edge case?

1 Answers1

0

I'm providing a sample below that answers multiple questions:

* def response =
"""
{
    "ABC": {
        "code": "ABC",
        "isActive": "true",
        "lastUpdatedBy": "username",
        "execution": {
            "status": "0",     
        },
        "priority": "1"
    },
    "DEF": {
        "code": "DEF",
        "isActive": "true",
        "lastUpdatedBy": "username",
        "execution": {
            "status": "1",     
        },
        "priority": "1"
    }
}
"""
# get all values in root json
* def items = $.*
* def code = 'ABC'
* def found = items.filter(x => x.code == code && x.execution.status == '0')
* assert found.length == 1
# the expression on the right below is pure JS
* match found[0] == response[code]

Note:

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Will it print that particular code? Also, can it be stored in an array and be use in a different eval statement? – Vivek Arora Feb 14 '22 at 08:05
  • Expected output from my example is : [ABC,JKL]. I want to achieve this by parsing the entire response using an array mentioned in the Background "all = [ABC,DEF,GHI,JKL]". So for all[i] I want to get the execution status as '0'. – Vivek Arora Feb 15 '22 at 11:17
  • print response["ABC"]["execution"]["status"] I want to parameterise the above statement to produce status for all the values from "all = [ABC,DEF,GHI,JKL]" – Vivek Arora Feb 15 '22 at 11:22
  • @VivekArora sorry I pass. I don't understand your question. someone else here may have more patience, all the best – Peter Thomas Feb 15 '22 at 13:36
  • I understand. I am trying to convey that in your answer: * def code = 'ABC' * def found = items.filter(x => x.code == code && x.execution.status == '0') I want to use code as an array and iterate that array so x.code == code should be like x.code == code[i]. Apologies for a lots of comments and confusion – Vivek Arora Feb 16 '22 at 08:32
  • @VivekArora yes all of that is possible. can you read the docs here, try some code and then ask a new question if you are stuck (with a clear example) - and please don't use JS for-loops: https://github.com/karatelabs/karate#json-transforms – Peter Thomas Feb 16 '22 at 08:39