Remove item from JSON list

Amy V2
Mega Sage

I'm trying to remove a specific item from a JSON list. Below is a generic example of a JSON list that gets created from an OOTB business rule when a user changes the variables on a form they submitted.

 

[{"questionLabel":"Item to Remove","questionCurrValue":"new variable value","questionPrevValue":"previous variable value"},{"questionLabel":"Second item in list","questionCurrValue":"Yes","questionPrevValue":"No"},{"questionLabel":"Third item in list","questionCurrValue":"second test","questionPrevValue":"first test"},{"questionLabel":"Hidden item in list","questionCurrValue":"NA","questionPrevValue":""}]

 

What I'd like to do is search for the question labeled Item to Remove, and remove that whole item from the list, so the resulting list would be:

 

[{"questionLabel":"Item to Remove","questionCurrValue":"new variable value","questionPrevValue":"previous variable value"},{"questionLabel":"Second item in list","questionCurrValue":"Yes","questionPrevValue":"No"},{"questionLabel":"Third item in list","questionCurrValue":"second test","questionPrevValue":"first test"},{"questionLabel":"Hidden item in list","questionCurrValue":"NA","questionPrevValue":""}]

 

Any help is appreciated. Thanks!

1 ACCEPTED SOLUTION

Nishant8
Giga Sage

Hello @Amy V2 , can you please try as below?

var data = [
  { questionLabel: "Item to Remove", questionCurrValue: "new variable value", questionPrevValue: "previous variable value" },
  { questionLabel: "Second item in list", questionCurrValue: "Yes", questionPrevValue: "No" },
  { questionLabel: "Third item in list", questionCurrValue: "second test", questionPrevValue: "first test" },
  { questionLabel: "Hidden item in list", questionCurrValue: "NA", questionPrevValue: "" }
];
// Filter out unwanted item
var filteredData = data.filter(item => item.questionLabel !== "Item to Remove");
gs.info(JSON.stringify(filteredData));

Regards,

Nishant

View solution in original post

5 REPLIES 5

Nishant8
Giga Sage

Hello @Amy V2 , can you please try as below?

var data = [
  { questionLabel: "Item to Remove", questionCurrValue: "new variable value", questionPrevValue: "previous variable value" },
  { questionLabel: "Second item in list", questionCurrValue: "Yes", questionPrevValue: "No" },
  { questionLabel: "Third item in list", questionCurrValue: "second test", questionPrevValue: "first test" },
  { questionLabel: "Hidden item in list", questionCurrValue: "NA", questionPrevValue: "" }
];
// Filter out unwanted item
var filteredData = data.filter(item => item.questionLabel !== "Item to Remove");
gs.info(JSON.stringify(filteredData));

Regards,

Nishant

Thanks Nishant, I will try this out. @John Gilmore is correct in that it's a variable holding an array. Going to try both answers.

John Gilmore
Giga Guru

You can use jsonArray.splice() to remove the object using its array index value. The .splice('index to start removing from', 'number of elements to remove'). With this in mind array indexes start with a value of 0 so the first object has an index of 0, the next an index of 1, and so on. This is good to understand even thought its not necessary as you can us .indexOf() method to return the index of any object. Applying this to you exact example above:

var jsonArray = [{"questionLabel":"Item to Remove","questionCurrValue":"new variable value","questionPrevValue":"previous variable value"},{"questionLabel":"Second item in list","questionCurrValue":"Yes","questionPrevValue":"No"},{"questionLabel":"Third item in list","questionCurrValue":"second test","questionPrevValue":"first test"},{"questionLabel":"Hidden item in list","questionCurrValue":"NA","questionPrevValue":""}];

var index = jsonArray.indexOf("Item to Remove");

jsonArray.splice(index, 1);

In actual use you will probably be passing the variable containing the array so instead of having the typed out array you would just set jsonArray (or whatever variable name you want to use) to the input.

If you are performing this action in a server side script you also have access to the ArrayUtil() class within Servicenow which provides the same indexOf() method but with more consistant behaviour as compatibility can sometimes be an issue within ServiceNow.

Its worth noting that the indexOf() method will always return the index of the first match it finds. If you need to find and remove multiple objects you will need to use loop logic to either identify the proper range of objects to be removed or delete multiple objects one at a time.

Hi John - Thanks for the answer. You are right there is a variable holding an array. I do understand how arrays are numbered and was trying to use indexOf to get the location of the specific questionLabel I'm looking for because it's not always at the beginning. In the code you provided, do I only need to search for one part of an element in an array (i.e. the variable label) to get the index of the bracketed items? In other words, the questionLabel:value, questionCurrValue:value, questionPrevValue:value are all within the same set of brackets and all need to be removed. If I do indexOf and splice using just the questionLabel's value, does that remove the entire element from the array?