# How to extract JSON properties with special characters in LogQL

**URL:** <https://community.grafana.com/t/how-to-extract-json-properties-with-special-characters-in-logql/86168>\
**Category:** Grafana Loki\
**Tags:** query-help, logql\
**Created:** [April 20, 2023, 1:53pm UTC](https://community.grafana.com/t/how-to-extract-json-properties-with-special-characters-in-logql/86168 "2023-04-20T13:53:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyrumx](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@kyrumx](https://community.grafana.com/u/kyrumx)\
**Post date:** [April 20, 2023, 1:53pm UTC](https://community.grafana.com/t/how-to-extract-json-properties-with-special-characters-in-logql/86168/1 "2023-04-20T13:53:13Z")

</div>

I am using Serilog CompactJsonFormatter which yields JSON log messages. In this case, the level is found in the ‘@l’ property:

```auto
{"@t":"2016-06-07T03:44:57.8532799Z","@mt":"Hello, {User}","User":"nblumhardt","@l":"Debug"}

```

However, using LogQL, I find myself unable to do this with properties that have a ‘@’ in them:

```auto
{app="test"} | json level="@l" | level = `Error`

```

Giving me a _cannot parse expression [@l]: syntax error: unexpected $end, expecting LSB or FIELD_ error.  
I cannot simply escape it with ‘\’, as this will give me another error:  
_parse error at line 1, col 66: invalid char escape. Make sure that all special characters are escaped with \._

How do I properly parse the ‘@l’ JSON property into a label?

---

<div class="post-metadata">

**Author:** ![davidallen5](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/davidallen5/32/32092_2.png) [@davidallen5](https://community.grafana.com/u/davidallen5)\
**Post date:** [April 20, 2023, 2:45pm UTC](https://community.grafana.com/t/how-to-extract-json-properties-with-special-characters-in-logql/86168/2 "2023-04-20T14:45:53Z")

</div>

Hi! So you’ve got the right syntax and approach on extracting a JSON property, you’re just missing a bit more fancy escaping format needed there.

Instead of `json level="@l"` do this:

```auto
json level=`["@l"]`

```

the `@` isn’t allowed in a label as it’s not in the list of valid characters for a Prometheus label, which are generally `0-9a-z-A-Z-_`, so the extra escaping is necessary because of prometheus label restrictions, not json, is I think the case here.

I have created a link to a spot where you can play with this syntax and see that it works, which is within the LogQL query analyzer documentation.

> **[LogQL Analyzer | Grafana Loki documentation](https://grafana.com/docs/loki/latest/logql/analyzer/?query=%7C%2Bjson%2Blevel%3D%60%5B%22%40l%22%5D%60&line%5B%5D=%7B%22%40l%22%3A%22INFO%22%7D)**
>
> The LogQL Analyzer is an inline educational tool for experimenting with writing LogQL queries.

---

<div class="post-metadata">

**Author:** ![kyrumx](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@kyrumx](https://community.grafana.com/u/kyrumx)\
**Post date:** [April 24, 2023, 6:17am UTC](https://community.grafana.com/t/how-to-extract-json-properties-with-special-characters-in-logql/86168/3 "2023-04-24T06:17:13Z")

</div>

Cheers! This is exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/grafana/original/2X/e/ef5308cc58c9e5a13187b5012eb99f406928462f.png) [@system](https://community.grafana.com/u/system)\
**Post date:** [April 23, 2024, 6:18am UTC](https://community.grafana.com/t/how-to-extract-json-properties-with-special-characters-in-logql/86168/4 "2024-04-23T06:18:09Z")

</div>

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
