How to extract values from a nested json object in Loki

Hello everyone,

  • What Grafana version and what operating system are you using?

  • What are you trying to achieve?
    I am trying to extract the values associated with a key from a log (the log is in json format), but I am having trouble extracting the values associated with a specific key, when that key is nested inside my log by a few layers, and the json parser doesn’t generate the correct field to access that key.

  • How are you trying to achieve it?
    I will try to give a simplified example of what I mean and what isn’t working.
    Say my json object looks like

{ "a" : 
    { "b" : 
            "c" : {"key1" : "first"},
            "d" : {"key2" : "second"},

and my goal is to extract the values of field “b” i.e. “c” : {“key1” : “first”} and “d” : {“key2” : “second”}. How can I do that using LogQL?

  • What happened?
    I tried using the json parser, but this only generates the fields a_b_c_key1 and a_b_d_key2, so until there is no more nesting, but I would need the field a_b. My ideal use case would be to write a query that returns the values of field “b” directly as json objects, but I don’t think this is possible in Grafana.

This example is a bit simplified to get my point across, so feel free to ask any clarifying questions.