Need to convert the REST message response from CSV to JSON
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2018 02:20 PM
Hi All,
I am getting the response body from REST message in text format(CSV). Can anyone please help me in converting the csv response to JSON.
Thanks in Advance.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2018 11:15 AM
Hi Deepak,
Thanks for your reply. I have modified some part of the code to remove extra characters \" from header and data. Now I am able to convert the data from .csv to json for most of the files. But still getting the incorrect data into variables for some of the files.
Below is my code and sample output that I am getting. Can you please help me with that.
Code:
var csvToJSON = [];
var headers = [];
dataAsString = responseBody;
gs.print("Original CSV Content " + dataAsString);
var patternString = /\r+/;
var arrayString = dataAsString.split(patternString);
var numberOfRows = dataAsString.split(patternString).length;
headers = [];
headers = arrayString[1].split(",");
gs.print("headerss " + headers);
headers = headers.map(function(h) {
h = h.trim()
h = h.replace(/\"/g, '');
return h;
});
for ( var i = 2 ; i < numberOfRows-1 ; i++ ) {
var row = {};
gs.print("row: " + i + ": " + arrayString[i]);
var rowStringArray = arrayString[i].split(/,(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)/);
//var rowStringArray = arrayString[i].split( /(".*?"|[^",\s]+)(?=\s*,|\s*$)/g);
for ( var m = 0 ; m < rowStringArray.length ; m++) {
var content = rowStringArray[m].trim();
content = content.replace(/\"/g, '');
row[headers[m]] = content;
}
csvToJSON.push(row);
}
gs.print(JSON.stringify(csvToJSON, null , 4));
Sample CSV Response:
"number","caller_id","location","sys_created_on","closed_at","short_description","category","priority","state","Type","Severity","Port","Protocol","FQDN","SSL","assignment_group","assigned_to"
"INC0010005","Alissa Mountjoy",,"2016-04-15 12:34:10",,"Unable to get to network file shares","Network","5 - Planning","New",,,
"INC0000002",,, "2014-07-18 15:30:06",,"Unable to get to network, file shares", "Network", "1 - Critical", "Awaiting Problem", "Network", "Howard Johnson"
Sample JSON Response:
*** Script: [
{
"number": "INC0010005",
"caller_id": "Alissa Mountjoy",
"location": "",
"sys_created_on ": "2016-04-15 12:34:10",
"closed_at": " ",
"short_description": "Unable to get to network file shares",
"category": "Network",
"priority": "5 - Planning",
"state": "New ",
"assignment_group": "",
"assigned_to": ""
},
{
"number": "INC0000002,,,2014-07-18 15:30:0,Unable to get to network",
"caller_id": "file shares,Network,1 - Critical,Awaiting Problem,Network,Howard Johnson,\n \nBelow are the sample versions: \n1.29,
"location": "32.45",
"sys_created_on": "67.89",
"closed_at": "96.34",
"short_description": "21.35",
"category": "67.112",
"priority": "23.12",
"state": "122.2",
"assignment_group": "59.30",
"assigned_to": "23.89",
"description": "13.76\n Please Note:\n This needs to be reported ASAP"
}
]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2018 12:40 PM
Sorry, I provided the wrong one for INC0000002. Please see the csv response below.
"INC0000002",,, "2014-07-18 15:30:06",,"Unable to get to network, file shares", "Network", "1 - Critical", "Awaiting Problem", "Network", "Howard Johnson"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2018 07:28 AM
Hello,
You can refer below and create your own code
https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data
Note: Please mark reply as correct / helpful if it answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2018 12:13 AM
I have created a blog which has a script include to convert CSV to JSON
Note: Please mark reply as helpful or correct if it answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2018 11:15 PM