ElasticSearch api解析嵌套字符串json仪表板

时间:2015-08-23 16:20:19

标签: json bash elasticsearch jq elasticsearch-api

我使用ElasticSearch GET获取dashbaord的json文件: 例如:http://ES_IP:9200/kibana-int/dashboard/my_Dashboard/

这会返回一个json文件,如:

{"_index":"kibana-int","_type":"dashboard","_id":"my_Dashboard","_version":5,"found":true,"_source":{ "user":"guest", "group":"guest", "title":"my_Dashboard", "dashboard":"{ \"title\": \"My Dashboard\", \"services\": { \"query\": { \"list\": { \"0\": { \"id\": 0, \"type\": \"lucene\", \"query\": \"type:dh AND severity:ERROR AND (response.baseUrl:\"/rm/recordings/*\" OR request.baseUrl:\"/rm/recordings/*\")\", \"alias\": \"DH errors rcc\",.......

这是我需要你的帮助的地方,我怎样才能获得关键的价值"仪表板"但没有逃脱的' \键/值对中的字符不影响作为值的一部分的转义?

我需要的输出应该是:

{ "title": "My Dashboard", "services": { "query": { "list": { "0": { "id": 0, "type": "lucene", "query": "type:dh AND severity:ERROR AND (response.baseUrl:\"/rm/recordings/*\" OR request.baseUrl:\"/rm/recordings/*\")", "alias": "DH errors rcc",.......

在查询键中注意,在其值中,有一些\"这不应该受到影响,因为它们是价值的一部分。

我需要输出然后在我的一些bash脚本中用jq解析json。

ElasticSearch api是否有一些过滤器为我提供输出? 或者你知道另一种外部方法来获得我需要的东西吗?

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

fromjson 是你的朋友。例如:

def data: {
  "_index": "kibana-int",
  "_type": "dashboard",
  "_id": "my_Dashboard",
  "_version": 5,
  "found": true,
  "_source": {
    "user": "guest",
    "group": "guest",
    "title": "my_Dashboard",
    "dashboard": "{ \"title\": \"My Dashboard\", \"services\": { \"query\": { \"list\": { \"0\": \"foobar\" }}}}"
  }
};

data | ._source.dashboard | fromjson

输出:

$ jq -n -f elastic.jq
{
  "title": "My Dashboard",
  "services": {
    "query": {
      "list": {
        "0": "foobar"
      }
    }
  }
}