Google App脚本。来自BigQuery.Jobs.query响应的JSON

时间:2015-12-05 18:19:55

标签: google-apps-script google-bigquery

我想从BigQuery.Jobs.query的响应中获取JSON字符串。有什么简单的方法吗?

Google App脚本

var request = {
    query: 'SELECT TOP(word, 300) AS word, COUNT(*) AS word_count ' + 'FROM publicdata:samples.shakespeare WHERE LENGTH(word) > 10;'
};
var queryResults = BigQuery.Jobs.query(request, projectId);

我想从queryResults

获取JSON文本
[
  {
    "word": "counterfeit",
    "word_count": "28"
  },
  {
    "word": "remembrance",
    "word_count": "24"
  },
  {
    "word": "countenance",
    "word_count": "24"
  },

感谢Henrique Abreu。

var request = {
  query: 'SELECT TOP(word, 300) AS word, COUNT(*) AS word_count ' + 'FROM publicdata:samples.shakespeare WHERE LENGTH(word) > 10;'
};
var queryResults = BigQuery.Jobs.query(request, projectId);
Logger.log(parseSimpleResults(queryResults));

[
   {
      word_count=28,
      word=counterfeit
   },
   {
      word_count=24,
      word=remembrance
   }
]

2 个答案:

答案 0 :(得分:4)

我没有使用更多BigQuery查询结果对此进行测试,但它应该适用于"简单"结果

function parseSimpleResults(results) {
  var names = results.schema.fields.map(function(field){ return field.name; });
  return results.rows.map(function(row) {
    var obj = {};
    for( var i = 0, len = names.length; i < len; ++i ) {
      obj[names[i]] = row.f[i].v;
    }
    return obj;
  });
}

只需将queryResults变量传递给它,就可以得到很好的对象数组。

答案 1 :(得分:0)

只是您探索的方向之一:

NSManagedObject