- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 07:56 AM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 08:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 09:47 AM
try this
// Your original JSON array as a string
var jsonString = '[{"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":""}]';
// Parse the string to a JS array
var jsonArray = JSON.parse(jsonString);
// The label you want to remove
var labelToRemove = "Item to Remove";
// Filter out the item(s) with that label
var filteredArray = jsonArray.filter(function(item) {
return item.questionLabel !== labelToRemove;
});
// Convert back to JSON string if needed
var resultString = JSON.stringify(filteredArray);
// Output (for testing)
gs.info(resultString);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader