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