How to extract data from response body (JSON & XML) in Postman
‘Why we want to extract data from response ?’
One of reason is to chain the API requests.
Now, what is API chaining?
API chaining is to extract data from one request, save it in variable and use the variable in another request. This will make our request more dynamic and reduce the manual task.
We can extract data from both JSON and XML response body.
Let’s dig in how to extract data from response body with following example:
Extracting data from JSON response body
Prerequisites:
· Postman
· GET request API URL: https://reqres.in/api/users?page=1
Step 1: Add request in postman
Notice we got our response in nested JSON object array schema.
Step 2: Parse the data from JSON response
· Go to the Tests
· Define a variable to parse the JSON data
Step 3: Extract the path of desired node from Nested JSON array
To extract data from JSON object array we can use any online tool like JSON Path Finder.
· Go to http://jsonpathfinder.com/
· Copy response from postman and paste it in JSON Path Finder
· Copy the path for desired node as shown below to use it in Postman
Examples:
· To get value of first_name: x.data[3].first_name
· To get the of last_name: x.data[3].last_name
Step 4: Use the path to extract data from response body
Just hit the Send button and console output shows that we Successfully extracted data from JSON response body
Extracting data from XML response body
Prerequisites:
· Postman
· Post request API WSDL: http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
Step 1: Add request in postman
Step 2: Parse the data from XML response
· Go to the Tests
· Define a variable to parse the XML data
Step 3: Extract the path of desired node from XML Response
Consider the XML Response as below:
For example to reach node ”m:CountryISOCodeResult”, we need to start from root node → soap: Envelope → soap: Body → m:CountryISOCodeResponse → m:CountryISOCodeResult
So the path for CountryISOCodeResult : [‘soap:Envelope’][‘soap:Body’][‘m:CountryISOCodeResponse’][‘m:CountryISOCodeResult’]
Step 4: Use the path to extract data from response body
Just hit the Send button and console output shows that we Successfully extracted data from XML response body
This is all about extracting data from JSON and XML response body. In the next blog I will use this extracted data for API chaining.
Happy Learning!