JSON data with hyphen in key not giving output

Tapish Sharma1
Kilo Sage

Hi All, 

I have a JSOn Response in following format .... I am able to extract data for Scotland key but not for 'england-and-wales'.....is this because there is a hyphen in the key , if yes how to process it

 

JSON

{
    "england-and-wales": {
        "division": "england-and-wales",
        "events": [
            {
                "title": "New Year’s Day",
                "date": "2018-01-01",
                "notes": "",
                "bunting": true
            },
            {
                "title": "Good Friday",
                "date": "2018-03-30",
                "notes": "",
                "bunting": false
            }
},
"Scotland": {
        "division": "Sctotland",
        "events": [
            {
                "title": "New Year’s Day",
                "date": "2018-01-01",
                "notes": "",
                "bunting": true
            },
            {
                "title": "Good Friday",
                "date": "2018-03-30",
                "notes": "",
                "bunting": false
            }
}
 
 
Code - (Works for scotland not for england)

try {
var r = new sn_ws.RESTMessageV2('Holiday calendar', 'Default GET');

//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var parsedData = JSON.parse(responseBody);





for(var i = 0; i<parsedData.england-and-wales.events.length;i++){
//gs.warn(parsedData.england-and-wales.events[i].title);


}

}
catch(ex) {
var message = ex.message;
}

 
1 ACCEPTED SOLUTION

Ahmmed Ali
Mega Sage

Hello,

 

You can read data as below:

 

var parsedData = JSON.parse(responseBody);
for(var key in parsedData){
var tempObj1 = parsedData[key];

//From here, you can read each attributes

var division = tempObj1.division;
}

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

2 REPLIES 2

Ahmmed Ali
Mega Sage

Hello,

 

You can read data as below:

 

var parsedData = JSON.parse(responseBody);
for(var key in parsedData){
var tempObj1 = parsedData[key];

//From here, you can read each attributes

var division = tempObj1.division;
}

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ahmmed, 

Your solution worked, just wondering whats wrong with my approach ? why does it work for scotland and not for england ?