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โ€
}
}