Javascript to promote top level lists
I just cannot seem to get this expression to work. Is purpose is to scan for top-level fields of an object and replaces any single-element array value (list) with just that one value. I do not want it to recurse.
Here are some examples I am looking for:
Input
{
"name": ["Alice"],
"roles": ["admin", "editor"],
"active": [true],
"profile": { "city": ["Springfield"] }
}
Output:
{
"name": "Alice", // promoted
"roles": ["admin", "editor"], // unchanged
"active": true, // promoted
"profile": { "city": ["Springfield"] } // untouched (no recursion)
}
I keep getting: Failure: The output document is a primitive value: null, Reason: The output document must be an array or object, Resolution: Check for target paths that write to the root
What am I missing?
{
promoteSingleArrays : (obj) =>
// Promote top-level single-item arrays of primitive types only
obj
.mapValues((val, key) =>
(Array.isArray(val) && val.length == 1 &&
(typeof val[0] == 'string' || typeof val[0] == 'number' || typeof val[0] == 'boolean'))
? val[0]
: val
)
}
I new if I posted I would see something:
Array.isArray(val) - This JavaScript method is not available in Snaplogic
I changed that to "val instanceof Array" and it works. Hope this helps someone.