how to get the REST API code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
20m ago
i am getting the rest api response like below
Response Body: {"id":"dsajnddna","id2":"orn","id3":"orndawdw"}
now i want get the id2 value from this body, i am unable to get the id2 value. please help mee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3m ago
If your response body is a JSON string:
var responseBody = '{"id":"dsajnddna","id2":"orn","id3":"orndawdw"}';
var obj = JSON.parse(responseBody);
var id2 = obj.id2; // "orn"
If your response body is already a JSON object:
var id2 = responseBody.id2;
If you need to guard against missing fields:
var id2 = (obj && obj.id2) ? obj.id2 : null;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
58 seconds ago
Hi,
This is assuming you are not looping through a list of these and just the response body you showed above, but you could try something like:
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
// Parse the JSON response
var jsonResponse = JSON.parse(responseBody);
// Extract values into variables
var id = jsonResponse.id;
var id2 = jsonResponse.id2;
var id3 = jsonResponse.id3;
gs.info('ID: ' + id + ', ID2: ' + id2 + ', ID3: ' + id3);
