Limiting, sorting and filtering json objects in Grafana Loki


I am storing event objects as jsons in Loki.


	"streams": [
			"stream": {
				"stream": "operational-events"
			"values": [
					"{"event_type": "test_event", "severity": "INFO", **additional_metadata, "data": {**event_data}}"

Then I would like to fetch these objects back. Is it possible to query in a way where can I sort events, limit them and filter them by the properties inside the json?

I went trough the docs but it does not seem like these features are possible.

I would expect query in this form: {stream=“operational-events”} | sort timestamp desc | limit 20

I am new to Grafana query language so I am not sure if I am missing something or these usecases are not supported.

Thanks for answers.

  1. Filtering is possible, please read LogQL | Grafana Loki documentation.

  2. It’s currently not possible to limit or sort within the query itself (you can do it externally with tool such as Grafana). There is a feature request for sorting, not sure about limit.