GET response to exclude null records

sukran
Mega Sage

We integrated external system data to ServiceNow and noticed number of records fields having 'null' value and that needs to exclude from payload

 

Response body [{"name":"system","list":"worker","id":"null"}]

whenever response body having id=null then it should exclude , how to achieve this? 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sukran 

you can parse the JSON response and skip the null records if id key has null value in it

Something like this, I considered 2 objects came and 1 has id=null and it gave me only 1

You can use that as reference point and continue your further logic

var response = [
  {"name":"system","list":"worker","id":"null"},
  {"name":"system2","list":"worker2","id":"123"}
];

// Filter out objects where id is "null" (as a string) or null (actual null)
var filtered = response.filter(function(item) {
  return item.id !== "null" && item.id !== null;
});

// Now 'filtered' will only contain objects with valid 'id' values
gs.info(JSON.stringify(filtered));

AnkurBawiskar_0-1749201480476.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Shruti
Mega Sage
Mega Sage

Hi,

Try to add filter to the endpoint/url

 

https://xxxx.service-now.com?$format=json&$filter=id ne null

We are pulling record from outside application and endpoint is not ServiceNow 

S0 , are you saying need to add external application url to modify with filter condition?

any method to do from ServiceNow side?

How are you pulling records?

If you have external application URL or endpoint just add filter to it

https://xxxx.external.url.com?$format=json&$filter=id ne null

Ankur Bawiskar
Tera Patron
Tera Patron

@sukran 

you can parse the JSON response and skip the null records if id key has null value in it

Something like this, I considered 2 objects came and 1 has id=null and it gave me only 1

You can use that as reference point and continue your further logic

var response = [
  {"name":"system","list":"worker","id":"null"},
  {"name":"system2","list":"worker2","id":"123"}
];

// Filter out objects where id is "null" (as a string) or null (actual null)
var filtered = response.filter(function(item) {
  return item.id !== "null" && item.id !== null;
});

// Now 'filtered' will only contain objects with valid 'id' values
gs.info(JSON.stringify(filtered));

AnkurBawiskar_0-1749201480476.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader