将JSON文档转换为谷歌图表格式

Converting JSON document to Google Chart format

本文关键字:表格 格式 谷歌 JSON 文档 转换      更新时间:2023-09-26

我是Javascript的新手,我的情况是这样的:我正在使用谷歌图表来可视化一些数据,数据包含在Elasticsearch中。

我正在用Ajax命令查询数据,但是返回的数据在当前格式的谷歌图表中不可用。

查询返回如下数据:

{ took: 5 timed_out: false _shards: { total: 5 successful: 5 failed: 0 } hits: { total: 11 max_score: 1 hits: [ { _index: inventory _type: on_hand _id: 4 _score: 1 _source: { warehouse_id: 107 date: 03-28-2013 lane: M01 routes: 383 } }

我需要为Google Charts设置如下格式:

{
  "cols": [
        {"id":"","label":"Lane","type":"string"},
        {"id":"","label":"Routes","type":"number"}
      ],
  "rows": [
        {"c":[{"v":"M01"},{"v":4657}]},
        {"c":[{"v":"M02"},{"v":4419}]},
        {"c":[{"v":"M03"},{"v":4611}]},
        {"c":[{"v":"M04"},{"v":4326}]},
        {"c":[{"v":"M05"},{"v":4337}]},
        {"c":[{"v":"M06"},{"v":5363}]}
      ]
}

虽然我不期望有人为我写代码,但如果有人能给我一个很好的起点,以提取所需的数据,并添加适当的格式,如"cols": [..."rows":[...等,我将非常感激。谢谢你!

编辑:

我能够运行一个更新的查询,以有效的JSON格式返回结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 7,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "4",
      "_score" : 1.0, "_source" : {"lane":"M04","routes":"102"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "5",
      "_score" : 1.0, "_source" : {"lane":"M03","routes":"143"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"lane":"M07","routes":"80"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "6",
      "_score" : 1.0, "_source" : {"lane":"M02","routes":"157"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "2",
      "_score" : 1.0, "_source" : {"lane":"M06","routes":"101"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "7",
      "_score" : 1.0, "_source" : {"lane":"M01","routes":"105"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "3",
      "_score" : 1.0, "_source" : {"lane":"M05","routes":"160"}
    } ]
  }
}

然而,所需的JSON文档实际上需要完全像我在谷歌图表的原始帖子中所显示的那样能够使用它。需要从返回的数据(如上所示)中提取"lane""routes"值,并将其格式化为原始文章中的JSON文档。再次感谢。

您应该能够做如下操作:

var json = {
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 1,
    "hits": [
      {
        "_index": "wcs",
        "_type": "routes",
        "_id": "4",
        "_score": 1,
        "_source": {
          "lane": "M04",
          "routes": "102"
        }
      }
    ]
  }
};
var data = {};
data.cols = [
    {
        "id": "",
        "label": "Lane",
        "type": "string"
    },
    {
        "id": "",
        "label": "Routes",
        "type":"number"
    }
];
data.rows = [
    {
        "c": [
            {
                "v": json.hits.hits[0]._source.lane
            },
            {
                "v": json.hits.hits[0]._source.routes
            }
        ]
    }
];
console.log(data);