0

is there some way to achieve the following behaviour in JMESPath?

I have a dict like this:

{
    "foo": "aa", 
    "bars": [
        "bb", 
        "cc"
    ]
}

I am looking for a result like the following. The scalar value "aa" should be combined with every item of the "bars" array to get an array of flat dicts. (The change from "bars" to "bar" would be nice but is not a must have, I look for the combination of the elements mainly.)

[
    {
        "foo": "aa",
        "bar": "bb"
    },
    {
        "foo": "aa",
        "bar": "cc"
    }
]

Would be great if someone knows a way to achieve this.

Christian
  • 41
  • 5

1 Answers1

0

This is currently impossible and would require to be able to access the parent object in an expression. The feature has been requested on github issue #22.


Right now you can create a list of objects not containing foo:

map(&{bar: @}, bars)

returns

[
  {
    "bar": "bb"
  },
  {
    "bar": "cc"
  }
]

Of course you could add foo with a static value like this:

map(&{foo: "aa", bar: @}, bars)

However, assuming the parent object feature will be implemented as variable $, as has been suggested in this github comment, you'd get the desired result using the expression

map(&{foo: $, bar: @}, bars)

Note that this is not working yet.

myrdd
  • 2,272
  • 1
  • 17
  • 22