-3

im working on an API request where i GET an http.response that is an array with one json object in it. But when i try to parse it or stringify it i with this code:

  var response = http.response;

try{
    var json = JSON.stringify(response);
    logInfo("status: " + json.status);
  } catch(e) { logInfo(e); }

...I get this log anwser: status: undefined

How do I define the value? The Value is taken from a user on my webpage and can change.

This is the response i GET from the request:

[
{
"status": "DONE",
"plannedDeliveryDate": "2017-06-27",
"orderId": 2112312,
"userId": 123123
}
]

This is the rest 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); }
}
  • 1
    If you want to parse JSON, why are you using `stringify` not `parse`? Also, we need to see the rest of your fetch code as it's most likely an issue to do with returning data from an async operation. – Andy Nov 16 '17 at 20:13
  • @Andy how does it matter it will still not work as he is trying to access array objects without indexes? – sumeet kumar Nov 16 '17 at 20:15
  • Well, it matters because any code to access the data _won't work if the data isn't parsed_. – Andy Nov 16 '17 at 20:17
  • i agree i'll update the answer – sumeet kumar Nov 16 '17 at 20:17
  • What is `HttpClientRequest`? – Kevin B Nov 16 '17 at 20:23
  • http://vertx.io/docs/apidocs/io/vertx/core/http/HttpClientRequest.html – Martin Gustafsson Nov 16 '17 at 20:30
  • The reason i ask is, typically ajax is asynchronous, and when it isn't, that's a bad idea. The way your code is written seems to assume it is synchronous. but if it were, you'd be getting different results. something isn't quite right here. – Kevin B Nov 16 '17 at 20:42
  • I also can't seem to find any documentation on the `execute` method you are calling. – Kevin B Nov 16 '17 at 20:44
  • To be honest, the system i write the code in is really old and only uses pure javascript. So I can understand that you never heard of execute before. – Martin Gustafsson Nov 16 '17 at 20:58
  • Uhh, is this JAVA? I don't see any of the classes you refer to in any javascript reference, and vertx *seems* to be a java library. Or maybe [JavaScript executed on the Adobe Campaign server.](https://docs.campaign.adobe.com/doc/AC/en/CFG_API_SOAP_methods_in_JavaScript.html)? – James Nov 16 '17 at 21:25
  • It doesn't look like "normal" JS, eg `for each (var line in res)` seems invalid – James Nov 16 '17 at 21:33

1 Answers1

-3

To parse json you use

JSON.parse(response);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

JSON.stringify() does the opposite, it converts data into json
Leon Radley
  • 7,596
  • 5
  • 35
  • 54