cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Detect XML attribute xsi:nil and apply null value

Thom
New Contributor II

I am using a SOAP Execute to retrieve information from an API. The payload is being converted to JSON. The problem is any field that is null returns with an attribute of @xsi:nil:โ€œtrueโ€. This is just an example subset. There are actually more than 10 fields returning like this.
[
{
โ€œns2:communityCodeDescโ€: {
โ€œ@xmlns:xsiโ€: โ€œhttp://www.w3.org/2001/XMLSchema-instanceโ€,
โ€œ@xsi:nilโ€: โ€œtrueโ€
},
โ€œns2:countyCodeDescโ€: {
โ€œ@xmlns:xsiโ€: โ€œhttp://www.w3.org/2001/XMLSchema-instanceโ€,
โ€œ@xsi:nilโ€: โ€œtrueโ€
},
โ€œns2:dateCreatedโ€: โ€œ2023-05-16T11:49:32.010-04:00โ€,
โ€œns2:dateObsoletedโ€: {
โ€œ@xmlns:xsiโ€: โ€œhttp://www.w3.org/2001/XMLSchema-instanceโ€,
โ€œ@xsi:nilโ€: โ€œtrueโ€
},
โ€œns2:familyRSNโ€: โ€œ1250โ€
}
]
I need this to be in a more standard JSON form like thisโ€ฆ
[
{
โ€œcommunityCodeDescโ€: null,
โ€œcountyCodeDescโ€: null,
โ€œdateCreatedโ€: โ€œ2023-05-16T11:49:32.010-04:00โ€,
โ€œdateObsoletedโ€: null,
โ€œfamilyRSNโ€: โ€œ1250โ€
}
]
So I need the xsi: construct converted to null and the namespace identifier dropped.
Any help would be appreciated.

1 ACCEPTED SOLUTION

Thom
New Contributor II

Expanding on the solution provided and using information from the web related to javascript, I came up with the following statement. There was experimentation involved for sure.
$.mapValues((value,key)=> typeof(value) == โ€˜objectโ€™ && value.hasOwnProperty(โ€œ@xsi:nilโ€)? null :value).mapKeys((value,key)=> key.replace(โ€˜ns2:โ€™,โ€˜โ€™))
mapValues and mapKeys are admittedly a bit of a black box for me still.
Thanks @JensDeveloper for leading me done a good path.

View solution in original post

4 REPLIES 4

JensDeveloper
Contributor II

Hi @Thom

I see that when its null itโ€™s always an object with two properties and when it has a value its type is a string. So I created a pipeline that checks which type it is and based on that it gets the value or puts null as the value. The pipeline below:
Soap_format_2023_06_22.slp (4.6 KB)

Let me know if it helps

Jens

Thom
New Contributor II

Thanks for this example. I was envisioning something that iterated through all the fields in the document and converting these objects to null. Do you believe that to be possible?
Any number of fields could be null and that could be different in each document being processed.
Thanks again.

Thom
New Contributor II

Expanding on the solution provided and using information from the web related to javascript, I came up with the following statement. There was experimentation involved for sure.
$.mapValues((value,key)=> typeof(value) == โ€˜objectโ€™ && value.hasOwnProperty(โ€œ@xsi:nilโ€)? null :value).mapKeys((value,key)=> key.replace(โ€˜ns2:โ€™,โ€˜โ€™))
mapValues and mapKeys are admittedly a bit of a black box for me still.
Thanks @JensDeveloper for leading me done a good path.

alchemiz
Contributor III

Hi Thom, just tried another approach using string manipulation ๐Ÿ˜Š

JSON.parse(JSON.stringify($).replace(/ns2:|\"@xmlns:xsi":"http:\/\/www.w3.org\/2001\/XMLSchema-instance"|"@xsi:nil":"true"/gm,'').replace(/\{,\}/gm,'null'))

image

Just in case a nested object comes along

e.g
image

image