How does the Custom (Parse by script) work in data sources?

einvaldserigsta
Kilo Sage

Hi,

I am trying to utilize the "Custom (Parse by script)" format for data sources in ServiceNow. I am retrieving a JSON-file through FTP, that does not match the formats described in the Docs

I am able to fetch each line and parse it to an object using 

var parser = new JSONParser();
var lineObj = parser.parse(line);

and then I have full access to the loaded data in the script.


However, I am not able to figure out how to use the "result" object, and its related functions to actually generate a record in the import table. Have anyone successfully used this method and can share how they were able to create the records in the import table using the result variable? 


The format i get data on is a list of objects seperated by new line, and no headers on first row:

{"object": {"var1":"123", "var2":"234"}}
{"object": {"var1":"2", "var2":"5"}}

The error message I get when loading is: No fields provided for mapping to headers

1 ACCEPTED SOLUTION

einvaldserigsta
Kilo Sage

I was able to figure out how to add headers and values using the script.

You need to create an object, then parse it to the result using "result.addRow(resultObj)".

E.g.

var parser = new JSONParser();
var lineObj = parser.parse(line);

var resultObj = { "header1": lineObj.value1, "header2": lineObj.value2};

result.addRow(resultObj);

View solution in original post

1 REPLY 1

einvaldserigsta
Kilo Sage

I was able to figure out how to add headers and values using the script.

You need to create an object, then parse it to the result using "result.addRow(resultObj)".

E.g.

var parser = new JSONParser();
var lineObj = parser.parse(line);

var resultObj = { "header1": lineObj.value1, "header2": lineObj.value2};

result.addRow(resultObj);