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-07-2018 04:55 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2018 03:27 PM
Hi Deepak,
Thanks for your reply. Please find the sample XML response body below.
"Variable1","Variable2","Variable3","Variable4","Variable5","Variable6","Variable7"
"var1value","var2value","var3value","var4value","var5value","var6value","var7value"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2018 03:28 PM
Sorry, CSV response body.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2018 11:10 AM
Hi Neha,
I have created a sample code and tested it as well, it is working fine. You may want to modify it little bit here and there but it should give you good start with your requirement. You might want to put validations as well which I believe you can easily do.
Note: Please mark reply as correct since it has answered your question
Output of the script:
*** Script: Original CSV Content "number","caller_id","location","sys_created_on","closed_at","short_description","category","priority","state","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","Alissa Mountjoy","United Kingdom","2014-07-18 15:30:06","","Unable to get to network file shares","Network","1 - Critical","Awaiting Problem","Network","Howard Johnson"
*** Script: headerss "number","caller_id","location","sys_created_on","closed_at","short_description","category","priority","state","assignment_group","assigned_to"
*** Script: row: 1: "INC0010005","Alissa Mountjoy","","2016-04-15 12:34:10","","Unable to get to network file shares","Network","5 - Planning","New","",""
*** Script: row: 2: "INC0000002","Alissa Mountjoy","United Kingdom","2014-07-18 15:30:06","","Unable to get to network file shares","Network","1 - Critical","Awaiting Problem","Network","Howard Johnson"
*** 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\"", "\"caller_id\"": "\"Alissa Mountjoy\"", "\"location\"": "\"United Kingdom\"", "\"sys_created_on\"": "\"2014-07-18 15:30:06\"", "\"closed_at\"": "\"\"", "\"short_description\"": "\"Unable to get to network file shares\"", "\"category\"": "\"Network\"", "\"priority\"": "\"1 - Critical\"", "\"state\"": "\"Awaiting Problem\"", "\"assignment_group\"": "\"Network\"", "\"assigned_to\"": "\"Howard Johnson\"" } ]
[0:00:00.009] Total Time
var csvToJSON = [];
var headers = [];
var gr = new GlideRecord("sys_attachment");
gr.get("f1daa7fa0ff70f0046f1cfdce1050e75"); // sys_id of file containing CSV
if (gr) {
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(gr.table_name, gr.table_sys_id);
var dataAsString = Packages.java.lang.String(bytesInFile);
dataAsString = String(dataAsString);
gs.print("Original CSV Content " + dataAsString);
var patternString = /\n+/;
var arrayString = dataAsString.split(patternString);
var numberOfRows = dataAsString.split(patternString).length;
headers = [];
headers = arrayString[0].split(",");
gs.print("headerss " + headers);
for ( var i = 1 ; i < numberOfRows - 1 ; i++ ) {
var row = {};
gs.print("row: " + i + ": " + arrayString[i]);
var rowStringArray = arrayString[i].split(",");
for ( var m = 0 ; m < rowStringArray.length ; m++) {
row[headers[m]] = rowStringArray[m];
}
csvToJSON.push(row);
}
gs.print(JSON.stringify(csvToJSON, null , 4));
}
Note: Please mark reply as correct since it has answered your question