VolkovLabs / volkovlabs-dynamictext-panel

Business Text Panel for @grafana
https://docs.volkovlabs.io
Apache License 2.0
78 stars 14 forks source link

Possible to pretty-print JSON? #144

Closed andness closed 1 year ago

andness commented 1 year ago

Hello and thanks for a brilliant plugin! I just came across it and it's a game changer for our use of Grafana.

I have some JSON in a Postgres JSON column that I would output in a pretty printed format. I was hoping this would be as simple as using the json function but I realized I'd misunderstood its use. So instead I tried this template:

```json
{{{event_data}}}
```

And in the query I use jsonb_pretty(event_data::jsonb)

The result is unfortunately just a big blob of text with the newlines rendered as \n, and if I try to use the non-sanitized version using just {{event_data}} it gets even uglier with " and such.

Is there a way to get nicely pretty printed json or am I out of luck?

andness commented 1 year ago

Oh never mind! I'm an idiot, the combination of jsonb_pretty(event_data::jsonb) and triple handlebars works, it was just that one of the values contained a stack trace that got rendered badly. You can close this.

Would be sweet to get syntax highlighting on the rendered json though :)

mikhail-vl commented 1 year ago

@andness Instead of pretty you can use JSON helper {{{json obj}}}, which we introduced recently: https://volkovlabs.io/plugins/volkovlabs-dynamictext-panel/helpers/#json

Screen Shot 2023-01-19 at 09 21 59

Thank you for suggesting it. I can take a look at how to add syntax highlighting similar to VSCode using Monaco Code Editor. I will try to look into it for the upcoming version.

andness commented 1 year ago

I'm struggling to get my head around it, part of it is probably because I have data with nested json in a string property so there's so many levels that need to be unwrapped.

So simplifying it down to the most basic example, this is what I get:

image

It seems the missing ingredient is a transformation:

image

Perhaps that should be in the documentation for the new json helper?

mikhail-vl commented 1 year ago

@andness Good point. The Postgres data source returns it as a string, not as an object, which we serialize using JSON.stringify.

Other data sources may return it as an Object like RSS/Atom Data Source, which we created it for. Then transformation is not required. I will update the documentation.

mikhail-vl commented 1 year ago

@andness Updated, let me know if there is anything else: https://volkovlabs.io/plugins/volkovlabs-dynamictext-panel/helpers/#transformation.

andness commented 1 year ago

Great! I must say I'm impresses with your speedy response! iIf only the support teams for the services we pay for were as good 😏

mikhail-vl commented 1 year ago

@andness We added syntax Highlighting and custom styles to override the default highlight theme from a highlight.js project.

It will be a part of today's release, 3.0.0.

Screen Shot 2023-02-19 at 08 17 07

mikhail-vl commented 1 year ago

@andness We released version 3.0.0 and sent it to the Grafana team for review. It usually takes a couple of days to review, and then it will be added to the Grafana Catalog.

Blog post with details: https://volkovlabs.io/blog/dynamictext-panel-3.0.0-20230219/ Meanwhile, you can download it from GitHub. Let me know if there is anything else.

andness commented 1 year ago

Great! Looking forward to trying it out! I'm planning on upgrading our Grafana to 9.4 as soon as that it is out so hopefully this will be available in the Catalog by then too.