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