diff --git a/docs/sources/logql/template_functions.md b/docs/sources/logql/template_functions.md index e28827842174..403ee51e70b7 100644 --- a/docs/sources/logql/template_functions.md +++ b/docs/sources/logql/template_functions.md @@ -595,3 +595,19 @@ Signature: `toFloat64(v interface{}) float64` ```template {{ "3.5" | float64 }} //output 3.5 ``` + +## fromJson + +> **Note:** Added in Loki 2.3. + +fromJson decodes a JSON document into a structure. If the input cannot be decoded as JSON the function will return an empty string. + +```template +fromJson "{\"foo\": 55}" +``` + +Example of a query to print a newline per queries stored as a json array in the log line: + +```logql +{job="cortex/querier"} |= "finish in prometheus" | logfmt | line_format "{{ range $q := fromJson .queries }} {{ $q.query }} {{ end }}" +``` diff --git a/pkg/logql/log/fmt.go b/pkg/logql/log/fmt.go index 0551d9557852..2b5f04d570a7 100644 --- a/pkg/logql/log/fmt.go +++ b/pkg/logql/log/fmt.go @@ -75,6 +75,7 @@ var ( "ceil", "floor", "round", + "fromJson", } )