cancel
Showing results for 
Search instead for 
Did you mean: 

JSON formating String

peter
New Contributor III

I have a pipeline that uses the ServiceNow API Snap to retrieve data about ServiceNow Tasks. The Snap returns the data and I pass through a mapper.

However the ServeirNow form that I am getting data from contains ServiceNow custom variables and these come back from the API as a string and I am struggling to get these into a JSON format.

This is the data after the mapper

{
“number”:“RITM123456”
“etc:123”
“variables”:“Auto ID = 123\n Autoversion ID = 345\n Autoname = SQL stuff\n etc…”
}

I have tried using split and replace, but I cannot get the data into the right format.

How do I get everything into a JSON format?

10 REPLIES 10

alchemiz
Contributor III

Hi Peter,

I just mocked the $obj, the sl.ensureArray is to cast $obj as array so that I can invoke toObject() function

So, to answer your question about the white-spaces… the toObject function is basically creating key/value pair… the 1st callback generates the key and the 2nd callback generates the value, all you need to do is use the script that defines the key and update what you have

toObject((x,y)=> x.split(’=’)[0].trim().replace(/\s/g,’_’), (x,y)=> x.split(’=’)[1].replace(/[CRLF]/g,’\r\n’))

~EmEm