jslt icon indicating copy to clipboard operation
jslt copied to clipboard

Add a filter() function that allow to selected keys and nested keys from an object

Open ecerulm opened this issue 6 years ago • 3 comments

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]

ecerulm avatar Nov 08 '18 15:11 ecerulm

Couldn't you just write [for (myobj) . if (.b or c.e)]?

larsga avatar Nov 30 '18 07:11 larsga

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.

ecerulm avatar Dec 04 '18 22:12 ecerulm

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.

larsga avatar Dec 05 '18 07:12 larsga