Martin
Martin

Reputation: 648

Why is Kibana dashboard definition escaped when stored into ElasticSearch

I want to make Kibana dashboard definitions shared using ElasticSearch but I found that the part defining dashboard is escaped in ES document

{
  "user" : "guest",
  "group" : "guest",
  "title": "Test",
  "dashboard": "{\"title\":\"Hardware Monitor\"}"
}

Why is that? It makes manual editing of dashboard definition unpleasant and error-prone.

Upvotes: 3

Views: 338

Answers (1)

Alain Collins
Alain Collins

Reputation: 16362

Look at the top level of the JSON document. It's a bunch of keys and values, with both in quotes, e.g.

"user" : "guest"

Now, they've put the document that describes the dashboard as a value for the "dashboard" key. If it were a simple string, it might look like this:

"dashboard" : "Good morning!"

But you can see that it's not a simple document - the dashboard is defined with its own JSON document.

"title" : "Hardware Monitor"

Note that this document is just another bunch of keys and values, with both in quotes, just like before.

Now, if you were to put them together simplistically, you'd get:

"dashboard" : ""title": "Hardware Monitor""

The JSON parser is looking for key values in quotes, so the beginning looks like this:

"dashboard" : ""

and you're left with a bunch of non-JSON after that.

With quotes, you don't really know if it's opening a new quotation or closing a previous one, so the parser needs some help from you.

That help comes in the form of escaping (removing the value of) the inner quotations, like this:

"name" : "William Royce \"Boz\" Scaggs"

Now the parser can easily tell where the value starts and ends.

If you're writing code, it's a common trick to use single quotes inside a pair of double quotes (or vice versa), like this:

grep '"Lido Shuffle"' file

would look for the string Lido Shuffle - surround by double quotes - in the given file.

This doesn't always work when dealing with random human input like Kibana is. The user might want to name their dashboard "Don't Look Here" or '8" of Rain', so the quoting can get weird quickly. Escaping the quotes is a generic way of dealing with it.

Hope that helps.

Upvotes: 1

Related Questions