Get json value with skipping 1st and 2nd level of dynamic value


#1

Just to give a context on what I want to achieve: I want to check if an existing “Test Cycle” exists in JIRA (Zephyr). If yes, then the test case should be executed under that Cycle ID. Else, a new Cycle will be created. I want to capture the Cycle ID if the Cycle name matches the one in Katalon
Example: response is “3”:{“name”=“ST Cycle 1”…) - from here, if “ST Cycle 1” exists in JIRA (Zephyr) then I want to capture the value “3”.

I parsed a json response using JsonSlurper (This code is under Keyword passed to Test Listener in Katalon so I can capture the testSuiteContext.getTestSuiteId():

//Capture the response from JIRA (Zephyr)
def search_test_cycle = WS.sendRequest(findTestObject('JIRA/ZAPI-Get List of Cycle', [('authorization') : GlobalVariable.JIRA_Authorisation, ('projectId') : GlobalVariable.JIRA_Project_ID, ('url') : GlobalVariable.JIRA_URL, ('versionId') : '', ('cycleId') : '', ('issueId') : '']))
//Parse the response into text
def json_search_test_cycle = jsonSlurper.parseText(search_test_cycle.getResponseText())
KeywordUtil.logInfo(json_search_test_cycle.toString())

The response (sorry it is long) is:

{-1=[{-1={build=, createdDate=, description=, endDate=, ended=, environment=, executionSummaries={executionSummary=[]}, expand=executionSummaries, isExecutionWorkflowEnabledForProject=true, isTimeTrackingEnabled=true, modifiedBy=, name=Ad hoc, projectId=14900, projectKey=TUT, startDate=, started=, totalCycleExecutions=0, totalExecuted=0, totalExecutions=0, versionId=-1, versionName=Unscheduled}, 13={build=, createdBy=sysadmin, createdByDisplay=System Administrator, createdDate=2019-01-30 17:41:12.403, description=Create cycle from Katalon, endDate=, ended=, environment=, executionSummaries={executionSummary=[]}, expand=executionSummaries, isExecutionWorkflowEnabledForProject=true, isTimeTrackingEnabled=true, modifiedBy=sysadmin, name=ST Cycle 1, projectId=14900, projectKey=TUT, sprintId=1, startDate=, started=, totalCycleExecutions=1, totalDefects=0, totalExecuted=0, totalExecutions=0, totalFolders=0, versionId=-1, versionName=Unscheduled}, recordsCount=2}]}

From the above response:
1st level: “-1” - although a negative number, this is always fixed
2nd level (dynamic): “-1” and “13” (id’s) with values of “name” as “Ad hoc” and “ST Cycle 1”, respectively

I want to capture “13” in this case.

Next set of codes as per @kazurayam 's response in another thread:

def expectedLabel = “ST Cycle 1”
def wantedValue = 0
for (Map map : json_search_test_cycle) {`
Set keySet = map.keySet()
Iterator iter = keySet.iterator()

while (iter.hasNext()) {
def key = iter.next()
def v = map.get(key)
if (v.get(‘name’) == expectedLabel) {
wantedValue = v.get(key)
WebUI.comment(“wantedValue=${wantedValue} when key=${key}”)
}
}
}

The problem I have is that the 2nd level is the key itself. I am unsure how to capture this.


Using JsonPath to read Web Service Response
#2

I want to read the HTTP response in a pretty-printed JSON format. Please change your code as follows and share what you get.

//Capture the response from JIRA (Zephyr) 
def search_test_cycle = ...
 
//Parse the response into text 
def json_search_test_cycle = jsonSlurper.parseText(search_test_cycle.getResponseText())

Import groovy.json.JsonOutput
String jsonStr = JsonOutput.toJson(json_search_test_cycle)
KeywordUtil.logInfo( JsonOutput.prettyPrint(jsonStr) )

#3

{
-1”:[
{
-1”:{
“totalExecutions”:0,
“endDate”:"",
“description”:"",
“totalExecuted”:0,
“started”:"",
“versionName”:“Unscheduled”,
“isExecutionWorkflowEnabledForProject”:true,
“expand”:“executionSummaries”,
“projectKey”:“TUT”,
“versionId”:-1,
“environment”:"",
“createdDate”:"",
“totalCycleExecutions”:2,
“isTimeTrackingEnabled”:true,
“build”:"",
“ended”:"",
"name":“Ad hoc”,
“modifiedBy”:"",
“projectId”:14900,
“startDate”:"",
“executionSummaries”:{
“executionSummary”:[
]
}
},
13”:{
“totalExecutions”:0,
“endDate”:"",
“description”:“Create cycle from Katalon”,
“versionName”:“Unscheduled”,
“projectKey”:“TUT”,
“totalDefects”:0,
“isTimeTrackingEnabled”:true,
“modifiedBy”:“sysadmin”,
“executionSummaries”:{
“executionSummary”:[
]
},
“totalExecuted”:0,
“started”:"",
“isExecutionWorkflowEnabledForProject”:true,
“sprintId”:1,
“expand”:“executionSummaries”,
“versionId”:-1,
“environment”:"",
“createdDate”:“2019-01-30 17:41:12.403”,
“totalCycleExecutions”:1,
“build”:"",
“createdBy”:“sysadmin”,
“ended”:"",
"name":“Environment Shakeout”,
“totalFolders”:0,
“projectId”:14900,
“createdByDisplay”:“System Administrator”,
“startDate”:""
},
"recordsCount":2
}]
}

If it helps, I highlighted the record count (’-1’.recordsCount.value) as well if I need to use it for a loop statement when trying to search for the right match.


#4

I am unable to read the text as a valid JSON.

Because the text you pasted in the above comment was digested by this Forum system and some characters were carelessly translated into other characters by the system. For example a double quotation character " was translated into “ or ” which looks similar but not identical at all. Therefore the text I find in the above comment is no longer a valid JSON.

Please re-post the text enclosing it with a pair of triple consecutive backtick characters:

```
paste your json text here
```

This is the syntax rule of Markdown how to present the text as is.


#5
{
    "-1": [
        {
            "-1": {
                "build": "",
                "createdDate": "",
                "description": "",
                "endDate": "",
                "ended": "",
                "environment": "",
                "executionSummaries": {
                    "executionSummary": [
                        
                    ]
                },
                "expand": "executionSummaries",
                "isExecutionWorkflowEnabledForProject": true,
                "isTimeTrackingEnabled": true,
                "modifiedBy": "",
                "name": "Ad hoc",
                "projectId": 14900,
                "projectKey": "TUT",
                "startDate": "",
                "started": "",
                "totalCycleExecutions": 2,
                "totalExecuted": 0,
                "totalExecutions": 0,
                "versionId": -1,
                "versionName": "Unscheduled"
            },
            "13": {
                "build": "",
                "createdBy": "sysadmin",
                "createdByDisplay": "System Administrator",
                "createdDate": "2019-01-30 17:41:12.403",
                "description": "Create cycle from Katalon",
                "endDate": "",
                "ended": "",
                "environment": "",
                "executionSummaries": {
                    "executionSummary": [
                        
                    ]
                },
                "expand": "executionSummaries",
                "isExecutionWorkflowEnabledForProject": true,
                "isTimeTrackingEnabled": true,
                "modifiedBy": "sysadmin",
                "name": "ST Cycle 1",
                "projectId": 14900,
                "projectKey": "TUT",
                "sprintId": 1,
                "startDate": "",
                "started": "",
                "totalCycleExecutions": 1,
                "totalDefects": 0,
                "totalExecuted": 0,
                "totalExecutions": 0,
                "totalFolders": 0,
                "versionId": -1,
                "versionName": "Unscheduled"
            },
            "recordsCount": 2
        }
    ]
}

#6

I made another post

In there you will find my proposal to your problem.