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

Looks like all you need is a mapper snap.

Screen Shot 2020-05-27 at 12.20.04 PM

alchemiz
Contributor III

Or in the scenario wherein the objects inside the variables object is dynamic you can always use the merge then filter variables to destroy the variables object ๐Ÿ˜„

image

peter
New Contributor III

Thanks for all your help.

I now have nearly what I am after. but how do I remove the white space and the beginning and end of the field names after the merge, i.e.

in the mapper I see;

$[โ€™ Additional Comments โ€˜]
$[โ€™ Risk Comments โ€˜]
$[โ€™ Automation author ']

and I would like to see
$Additional Comments or $Additional_Comments
$Risk Comments or $Risk_Comments
$Automation author or $Automation_author

Thanks
Peter

alchemiz
Contributor III

first trim the key then do a replace of spaces with underscore

$obj = " word of the day = my very energetic mother just serve us nine pie[CRLF][CRLF]HeHeheHeHehEhe "

sl.ensureArray($obj).toObject((x,y)=> x.split(โ€˜=โ€™)[0].trim().replace(/\s/g,โ€˜_โ€™), (x,y)=> x.split(โ€˜=โ€™)[1].replace(/[CRLF]/g,โ€˜\r\nโ€™))

image

peter
New Contributor III

I dont quite following the link above?

What is the sl in sl.ensure Array?

I switched $obj for my variable name.

I get the following error when I try, Could not compile expression: sl.ensureArray($variables).toO โ€ฆ (Reason: Invalid token: โ€˜โ€™โ€™ for expression: sl.ensureArray($variables).toO