August 11, 2020

How to extract JSON response and chain in next API request



There are two ways to get extract JSON response from one API response and chain it to another API request-

1:Using Property Transfer step- Step 1 : Add the api requests in a test case


for example- I have added two requests from Reqres.in API


1. listUsers


2. Updateuser


I am getting value from response listusers endpoint and that becomes request for updateuser endpoint
Step 2 : Add a property transfer step Step 3 : Create a new property transfer
Provide source and target details
As in the source you can see i have provided $data[2].first_name (we can get it from json path finder by giving specific node)


As in the target I have given $.name


Step 4 : Provide json path in source and target References http://jsonpathfinder.com/
Chrome extension : Json path finder


Step 5 : Run and check the results


Here is the output of UpdateUser endpoint-






2. Using Groovy script-


These are the steps you can add to groovy script-
JsonSlurper https://groovy-lang.org/json.html def resp = testRunner.testCase.getTestStepByName('ListUsers').getPropertyValue('response') def jsonSlurper = new groovy.json.JsonSlurper() def object = jsonSlurper.parseText(resp) def value = object.data[5].first_name testRunner.testCase.getTestStepByName('MyProperties').setPropertyValue('user',value)


As you can see in this image-






After adding these steps, you will be able to see in the properties the value from this listuser response will be given to user variable.









Now we need to set that property in updateuser request like-


${Properties#user} instead of name value as are getting from listuser response.


when we run our test, it is passed successful.


Thanks.

2 comments:

Please let me know if you have any doubts.

Key concepts in Pipeline

 1. Agent- To build your code or deploy your software using azure pipelines, you need at least one agent. Two types of agent- Microsoft host...