VMware Cloud Community
segadson
Contributor
Contributor

JSON.parse a tag with "-" in it

I am trying to do a request but one of the tags has a "-" in it.  How to I retrieve that attribute

Code is below:

var restAction = "/login/";

var postData = "username=test&password=xxxx";

var request = restHost.createRequest("POST",restAction,postData);

request.contentType = "application/x-www-form-urlencoded";

var response = request.execute();

System.log(response.statusCode);

if (response.statusCode < 202) {

  //this is ok

  JSONdata = response.contentAsString;

  System.log(JSONdata);

} else {

  throw("Return code: "+response.statusCode + " Response body: "+response.contentAsString);

}

var jsonData = JSON.parse(JSONdata);

System.log(jsonData.x-token);

Response:

[2016-10-19 10:27:08.650] [I] 200

[2016-10-19 10:27:08.650] [I] {

  "Set-Cookie": "token123456"; expires=Wed, 19 Oct 2016 22:22:20 GMT; Path=/",

  "result": "authenticated",

  "username": "o361404",

  "x-token": "token123456"

}

2016-10-19 10:36:32.756] [I] ReferenceError: "token" is not defined. (Workflow:GNOME / Scriptable task (item1)#16)

How do I parse the "x-token" attribute

0 Kudos
1 Reply
rmav01
Enthusiast
Enthusiast

Try:

System.log(jsonData['x-token']);

0 Kudos