A newer version of this documentation is available.

View Latest

Function: removeNullObjectsAndElements

    March 23, 2025
    + 12

    Goal: Recursively remove all nulls and empty items from a document.

    • This function removeNullObjectsAndElements will recursively prune nulls and empty items from a given JSON document.

    • Requires Eventing Storage (or metadata collection) and a "source" collection.

    • Will operate on all documents where doc.type === "my_data_to_prune".

    • This function is a superset of the cleanup functionality found in the Scriptlet removeObjectStubs.

    javascript
    // To run configure the settings for this Function, removeNullObjectsAndElements, as follows: // // Version 7.1+ // "Function Scope" // *.* (or try bulk.data if non-privileged) // Version 7.0+ // "Listen to Location" // bulk.data.source // "Eventing Storage" // rr100.eventing.metadata // Binding(s) // 1. "binding type", "alias name...", "bucket.scope.collection", "Access" // "bucket alias", "src_col", "bulk.data.source", "read and write" // // Version 6.X // "Source Bucket" // source // "MetaData Bucket" // metadata // Binding(s) // 1. "binding type", "alias name...", "bucket", "Access" // "bucket alias", "src_col", "source", "read and write" function removeNullParts(obj) { // remove any null Objects Object.keys(obj).forEach(k => (obj[k] && typeof obj[k] === 'object') && removeNullParts(obj[k]) || (!obj[k] && obj[k] !== undefined) && delete(obj[k]) // 6.6+ can use "delete obj[k]"" ); Object.keys(obj).forEach(k => (obj[k] && Array.isArray(obj[k])) && (obj[k] = obj[k].filter(element => element !== null)) ); return obj; } function removeEmptyParts(obj) { if (obj !== null && typeof obj == "object") { Object.entries(obj).forEach(([k, v]) => { if (obj[k] && typeof obj[k] === 'object') { // recurse removeEmptyParts(obj[k]) // collapse out null's in Array if any if (obj[k] && Array.isArray(obj[k])) { obj[k] = obj[k].filter(element => element !== null) } // remove empty [] array items and {} object items if ( (obj[k] && Array.isArray(obj[k]) && obj[k].length === 0) || (obj[k] && !Array.isArray(obj[k]) && Object.keys(obj[k]).length === 0) ) { delete(obj[k]) // 6.6+ can use "delete obj" } } }); } else { // obj is a number or a string } return obj; } function OnUpdate(doc, meta) { if (doc.type !== "my_data_to_prune") return; // make a new doc without any nulls var newdoc = removeNullParts(doc); // make a new doc without {} [] empties newdoc = removeEmptyParts(newdoc); // alias src_col to our source collection. Since rev 6.5+ allowed to be in r/w mode src_col[meta.id] = newdoc; }