jmespathgrafana-lokipromtail

Does JMESPath support something like a spread operator?


Is it possible to convert this JSON:

{ "message": "Hello!", "contextMap": { "foo": 1, "bar": 2, ...otherInContextMap }, ...someOther }

(where keys of contextMap are not known) into the following JSON:


{ "message": "Hello!", "foo": 1, "bar": 2, ...otherInContextMap  }

using JMESPath?

(Context: I want to scrape Java logs with contextMap using Promtail and store message together with members of contextMap to Loki.)

EDIT: I need to explicitly pick message and members of contextMap. (There are some other unknown/dynamic members ...someOther in JSON.)


Solution

  • You can merge a basic query selecting the content of contextMap along with a multiselect hash for the message.

    So, given the query:

    merge({message: message}, contextMap)
    

    On your given input, it would yield the expected:

    {
      "message": "Hello!",
      "foo": 1,
      "bar": 2
    }