'Get specific value from OData response in Postman
Can anyone of you can help me this: i Have such response body from WebAPI:
{
"value": "Created: \"salesorder\" : \"22a734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_anschlussadresses
Found 1 vis1_postleitzahls
Reusing: \"vis1_postleitzahl\" : \"0d9344c7-a45d-e711-80c5-955c5ca2a164\"
Found 1 vis1_orts
Reusing: \"vis1_ort\" : \"92734f57-375e-e711-80c5-955c5ca2a164\"
Created: \"vis1_anschlussadresse\" : \"67a734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_anschlussobjekts
Created: \"vis1_anschlussobjekt\" : \"6ba734c3-bf5f-ec11-80e6-0050568d2958\"
Found 0 vis1_infrastrukturinformations
Created: \"vis1_infrastrukturinformation\" : \"6fa734c3-bf5f-ec11-80e6-0050568d2958\"
Found 1 contacts
Reusing: \"contact\" : \"22530f60-285f-ec11-80e6-0050568d2958\"
Found 1 competitors
Reusing: \"competitor\" : \"7841f8e7-c211-ea11-80cd-0050568d3968\"
[0000]: Information: OK
"
}
i'd like to get specific values from this response body e.g. 22a734c3-bf5f-ec11-80e6-0050568d2958 and store as a environment variable. Is it possible?
Solution 1:[1]
The first observation is that the response is a standard serialized structure. Even if we deserialise the JSON payload, the value of value is this string, that seems to represent a form of execution log:
Created: "salesorder" : "22a734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_anschlussadresses
Found 1 vis1_postleitzahls
Reusing: "vis1_postleitzahl" : "0d9344c7-a45d-e711-80c5-955c5ca2a164"
Found 1 vis1_orts
Reusing: "vis1_ort" : "92734f57-375e-e711-80c5-955c5ca2a164"
Created: "vis1_anschlussadresse" : "67a734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_anschlussobjekts
Created: "vis1_anschlussobjekt" : "6ba734c3-bf5f-ec11-80e6-0050568d2958"
Found 0 vis1_infrastrukturinformations
Created: "vis1_infrastrukturinformation" : "6fa734c3-bf5f-ec11-80e6-0050568d2958"
Found 1 contacts
Reusing: "contact" : "22530f60-285f-ec11-80e6-0050568d2958"
Found 1 competitors
Reusing: "competitor" : "7841f8e7-c211-ea11-80cd-0050568d3968"
[0000]: Information: OK
To specifically resolve the value 22a734c3-bf5f-ec11-80e6-0050568d2958 we need to make a few assumtions:
- the value we are looking for is in the First line
- the value is contained inside double quotes
- the quoted value is the entire line content after the last full-colon character
; - the name of the environment variable is
FirstGuid
Given those assumptions, we can write a Tests script to run after the request:
var logValue = pm.response.json().value;
var lines = logValue.split(/\r?\n/);
var lineOneTokens = lines[0].split(':');
var guid = lineOneTokens[lineOneTokens.length() - 1].trim().replace(/"/g,"");
pm.environment.set('FirstGuid', guid);
References:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris Schaller |
