We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

split multi line string and create json

went james
Kilo Explorer

i want to split the string from \n\n and converted it into json object in Business Rule script

"VM to be provisioned for which region? = APAC\n\nLease Type = Temporary\n\nStart = 2021-04-07 14:53:17\n\nEnd = 2021-06-06 14:53:19\n\nSize = Basic A0 | 1 Core | 768 MB\n\nUsed for = Development\n\nBusiness purpose = test\n\n"

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@went james 

try this

Script:

var str = "VM to be provisioned for which region? = APAC\n\nLease Type = Temporary\n\nStart = 2021-04-07 14:53:17\n\nEnd = 2021-06-06 14:53:19\n\nSize = Basic A0 | 1 Core | 768 MB\n\nUsed for = Development\n\nBusiness purpose = test\n\n";

var arr = str.split('\n\n');

var obj = {};

for(var i=0;i<arr.length;i++){

var str1 = arr[i];

if(str1){
var arr1 = str1.split('=');

var key = arr1[0].trim();
var value = arr1[1].trim();

obj[key] = value;

}
}

gs.info(JSON.stringify(obj));

Output:

[0:00:00.065] Script completed in scope global: script


Script execution history and recovery available here


*** Script: {"VM to be provisioned for which region?":"APAC","Lease Type":"Temporary","Start":"2021-04-07 14:53:17","End":"2021-06-06 14:53:19","Size":"Basic A0 | 1 Core | 768 MB","Used for":"Development","Business purpose":"test"}

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron

Hi,

you can split the string using this -> split('\n\n')

but how the object should look like?

Should it be like this?

{

"VM to be provisioned for which region?":"APAC",

"Lease Type":"Temporary"

}

Regards
Ankur

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

yes object should look like 

{

"VM to be provisioned for which region?":"APAC",

"Lease Type":"Temporary"

}

---------------

i tried it 

myArray = value.split("\r\n"); ///(\r\n|\r|\n)/


for(var i =0; i<= myArray.length; i++){


var json = new JSON();
var str = myArray[i];
var data = json.encode(str);
finalArray.push(data);

}

@went james 

I anticipated that and have shared the working solution below

please check

Regards
Ankur

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

Ankur Bawiskar
Tera Patron

@went james 

try this

Script:

var str = "VM to be provisioned for which region? = APAC\n\nLease Type = Temporary\n\nStart = 2021-04-07 14:53:17\n\nEnd = 2021-06-06 14:53:19\n\nSize = Basic A0 | 1 Core | 768 MB\n\nUsed for = Development\n\nBusiness purpose = test\n\n";

var arr = str.split('\n\n');

var obj = {};

for(var i=0;i<arr.length;i++){

var str1 = arr[i];

if(str1){
var arr1 = str1.split('=');

var key = arr1[0].trim();
var value = arr1[1].trim();

obj[key] = value;

}
}

gs.info(JSON.stringify(obj));

Output:

[0:00:00.065] Script completed in scope global: script


Script execution history and recovery available here


*** Script: {"VM to be provisioned for which region?":"APAC","Lease Type":"Temporary","Start":"2021-04-07 14:53:17","End":"2021-06-06 14:53:19","Size":"Basic A0 | 1 Core | 768 MB","Used for":"Development","Business purpose":"test"}

Regards
Ankur

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