โ05-24-2020 06:07 AM
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?
โ05-26-2020 07:03 AM
You can use the .toObject method of the array
โ05-27-2020 03:31 AM
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?
โ06-01-2020 09:29 PM
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โ)) ๐
โ05-27-2020 07:41 AM
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โ
}
}