jslt
jslt copied to clipboard
Add a filter() function that allow to selected keys and nested keys from an object
Given the input :
{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}
I want the filter(myobj, ['.b', '.c.e'])
to produce the output
{
"b": 2,
"c": {
"e": 4
}
}
Note that is this different from [for (myobj) . if ('.b', '.c.e')]
that would produce an array [2, 4]
Couldn't you just write [for (myobj) . if (.b or c.e)]
?
But [for (myobj) . if ('.b', '.c.e')]
would return an array
[2,4]
not an object
.
I guess it's possible to use the Object for expressions to achieve filtering on the first level:
{for (myobj) .key : .value if .key == "b" }
but get's very cumbersome (or maybe I'm missing an obvious solution) when you want to select nested keys.
The obvious alternative to filter(myobj, ['.b', '.c.e']
is to write:
{
"b": .b,
"c": { "e": .c.e}
}
which is what I'm trying to avoid. I wanted a alternative syntax for this that is more concise just for the case when I'm selecting/dropping stuff without transforming the values.
I missed that you want to extract part of the structure, and not just the values. My bad.
Ok, I need to think more about this.