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

You can use the .toObject method of the array
image

image

peter
New Contributor III

Thanks very much.

I dont understand how the toObject works.

When I use the code I get an error for one record;
Cannot access element 1 of ‘x.split(’=‘)’, which has only 1 elements,

The issue seems to be with a description field which has line returns, i.e.

Description = Great things\r\nare a foot

The code is expect a new field after the \n hence do I need to replace the \r\n with a space?

Is there a good way to do this?

alchemiz
Contributor III

On top of my head you can replaceAll the ‘\r\n’ with a keyword e.g. ‘[CRLF]’ this will be a place holder for you to revert it back when doing the toObject e.g.

$variables.replaceAll(‘\r\n’,‘[CRLF]’).split(‘\n’)…toObject((x,y)=>…], (x,y)=>x.split(‘=’)[1].replaceAll(‘[CRLF]’,‘\r\n’)) 😄

peter
New Contributor III

I am nearly there with my pipeline however I now have “nested” JSON, that is the first set of JSON returned by the API and the variables. Is there a way to flaten so that I have a single level of JSON?

I now have:
{
“number”:“RITM123456”,
“etc:123”,
variables : {
“Auto ID : 123”
“Autoversion ID : 345”
“Autoname : SQL stuff”
}
}

And I would like

{
“number”:“RITM123456”,
“etc:123”,
“Auto ID : 123”
“Autoversion ID : 345”
“Autoname : SQL stuff”
}
}