How to load .txt file in data source and transform it please any help is appreciated

ABHAY_M
Tera Guru

find_real_file.png

 this is the sample data of text file  first coloumn represents location field and second column asset tag how to load this data in data source if parsing then how do i parse with script 

Thank you 

1 ACCEPTED SOLUTION

Jon23
Mega Sage

One approach is to use the 'Custom (Parse by Script)' format for a data source.

Based on the file you provided, the following parsing script can be used in the data source:

Parsing Script

// The input value can be accessed through the variables named "line", "lineNumber" and "result"
// The function uses result variable to return parse result back. 
(function(line, lineNumber, result) {

    var map = {};
    var items = line.split("|");
    map['location'] = items[0];
    map['asset'] = items[1];
    result.addRow(map);

})(line, lineNumber, result);

Data Source Record

find_real_file.png

 

 

View solution in original post

13 REPLIES 13

i have tried with csv file but unable use multiple delimiter as it is saperated by tab and '|' i just cannot import it

 

Hi,

you should be able to use the delimiter as pipe

tab character is not shown

So it would be difficult to load

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Jon23
Mega Sage

One approach is to use the 'Custom (Parse by Script)' format for a data source.

Based on the file you provided, the following parsing script can be used in the data source:

Parsing Script

// The input value can be accessed through the variables named "line", "lineNumber" and "result"
// The function uses result variable to return parse result back. 
(function(line, lineNumber, result) {

    var map = {};
    var items = line.split("|");
    map['location'] = items[0];
    map['asset'] = items[1];
    result.addRow(map);

})(line, lineNumber, result);

Data Source Record

find_real_file.png

 

 

Thank you very much @jwalton  i searched it like anything over the internet for servicenow but i couldnt found it thank you very much  wish You all the best 

@Jon23 Thanks for sharing this, adding to your script I have utilized this concept to parse midserver logs.

Since its in .txt format I wanted a way to convert that to data source and import set table to better analyze the data and I didnt want to manually convert it to csv or touch the file, below is the script I created to achieve that.

// The input value can be accessed through the variables named "line", "lineNumber" and "result"
// The function uses result variable to return parse result back.
(function(line, lineNumber, result) {

var map = {};
var items = line.split(" ");
map['datetime'] = items[0];
var items1 = line.split(" ");
map['level'] = items1[0];
map['message'] = items1[1];
result.addRow(map);

})(line, lineNumber, result);