Currently, I am working on an API request where I receive an HTTP response that contains an array with a single JSON object. However, when attempting to parse or stringify it using the following code:
var response = http.response;
try{
var json = JSON.stringify(response);
logInfo("status: " + json.status);
} catch(e) { logInfo(e); }
...I'm getting the following log output: status: undefined
I am wondering how I can define the value in this scenario. The value is obtained from a user on my webpage and may change.
This is the response I receive from the request:
[
{
"status": "DONE",
"plannedDeliveryDate": "2017-06-27",
"orderId": 2112312,
"userId": 123123
}
]
Below is the remaining part of my GET code:
loadLibrary("cus:json3.js");
var query = xtk.queryDef.create(
<queryDef operation="select" schema={vars.targetSchema}>
<select>
<node expr="@userNumber"/>
</select>
</queryDef>
);
var res = query.ExecuteQuery();
//Buffer the auth key
var buffer = new MemoryBuffer();
buffer.fromString("username:password", "utf-8");
for each (var line in res) {
var http = new HttpClientRequest();
http.header["Authorization"] = "Basic " + buffer.toBase64(); //Basic Auth
this.baseURL = "https://url..../data?user_id=" + line.@userNumber;
http.url = this.baseURL;
http.method = "GET"; //The GET request
http.execute();
var response = http.response;
try{
var json = JSON.parse(response);
logInfo("status: " + json[0].status);
} catch(e) { logInfo(e); }
}