【问题标题】:Issue with JSON object nested in an Array when inserting in BigQuery在 BigQuery 中插入时嵌套在数组中的 JSON 对象出现问题
【发布时间】:2019-01-27 16:21:53
【问题描述】:

我很难理解我正在做的嵌套中出了什么问题。 我想使用他们的 Nodejs 客户端在 Google Big Query 中插入一行。为此,我需要推送一个嵌套在数组中的 json 对象,如下所示:

[ { timestamp: '1533564208', device_id: '2nd_test', temperature: '20.0' } ]

当在我的代码中硬写这个时,我可以毫无问题地在 Big Query 中添加一行。

const rows = [
{ timestamp: '1533564208', device_id: '2nd_test', temperature: '20.0' }
];

bigquery
.dataset(datasetId)
.table(tableId)
.insert(rows)

现在我真正想做的是将我从 pubsub 获得的有效负载插入到 Big Query 中,这就是我面临问题的地方。 pubsub 数据的管理方式如下:

var payload = Buffer.from(pubsubMessage.data, 'base64').toString();
console.log(payload);
// [ { timestamp: '1533564208', device_id: '2nd_test', temperature: '20.0' } ] 

根据日志,它应该可以工作 - 它是一个嵌套在数组中的 JSON 对象,但是当它调用 Big Query API 时,它会遇到 Invalid value 错误:

error: ERROR: { ApiError: Invalid value at 'rows[0].json' (type.googleapis.com/google.protobuf.Struct), "[ { timestamp: '1533564208', device_id: '2nd_test', temperature: '20.0' } ]"
at Object.parseHttpRespBody (/Users/marion/google_iot_core_test/google_function/timeseriesBigQuery/node_modules/@google-cloud/bigquery/node_modules/@google-cloud/common/src/util.js:193:30)
at Object.handleResp (/Users/marion/google_iot_core_test/google_function/timeseriesBigQuery/node_modules/@google-cloud/bigquery/node_modules/@google-cloud/common/src/util.js:131:18)
at /Users/marion/google_iot_core_test/google_function/timeseriesBigQuery/node_modules/@google-cloud/bigquery/node_modules/@google-cloud/common/src/util.js:496:12
at Request.onResponse [as _callback] (/Users/marion/google_iot_core_test/google_function/timeseriesBigQuery/node_modules/retry-request/index.js:198:7)
at Request.self.callback (/Users/marion/google_iot_core_test/google_function/timeseriesBigQuery/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/Users/marion/google_iot_core_test/google_function/timeseriesBigQuery/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
code: 400,
errors: 
 [ { message: 'Invalid value at \'rows[0].json\' (type.googleapis.com/google.protobuf.Struct), "[ { timestamp: \'1533564208\', device_id: \'2nd_test\', temperature: \'20.0\' } ]"',
   domain: 'global',
   reason: 'badRequest' } ],
response: undefined,
message: 'Invalid value at \'rows[0].json\' (type.googleapis.com/google.protobuf.Struct), "[ { timestamp: \'1533564208\', device_id: \'2nd_test\', temperature: \'20.0\' } ]"' }

编辑:我深入研究了什么值会给我 rows[0] 并且由于某种未知的原因它似乎将它视为一个字符串并只返回以下内容

console.log(rows[0]);
// [

而不是

// { timestamp: '1533564208', device_id: '2nd_test', temperature: '20.0' }

但是当我尝试 JSON.parse 时,它​​会发出一个错误。

知道问题是什么吗?

谢谢!

【问题讨论】:

  • rows[0] 对象没有属性json
  • 我有点想通了,所以我尝试了几件事:让有效负载只是一个 JSOn 对象,然后将其推送到数组中,添加 JSON.parse。也没有用。
  • 奇怪的是它在 'rows[0].json' 处显示无效值,尽管之后指定的值似乎完全有效:"[ { timestamp: '1533564208', device_id: '2nd_test',温度:'20.0'}]"
  • 我认为它是无效的 json,如果它在一个字符串中,除非键 以及 值被引用?
  • 你说得对!我试图理解为什么......我发布了另一个问题:stackoverflow.com/questions/51953504/…

标签: javascript arrays json node.js google-bigquery


【解决方案1】:

正如 cmets 中提到的,问题是我没有发送有效的 JSON 字符串。 难以理解的是,在代码中硬编码以下 2 个版本会给我相同的结果并且可以正常工作。

{ timestamp: "1533564208", device_id: "2nd_test", temperature: "20.0" } 

'{"timestamp":"1533564208","device_id":"2nd_test","temperature":"20.0"}'

但在使用 PubSub Gcloud 和 Buffer 功能时,我必须确保传入

'{"timestamp":"1533564208","device_id":"2nd_test","temperature":"20.0"}' 

而不是

{ timestamp: "1533564208", device_id: "2nd_test", temperature: "20.0" } 

否则它不会认为它是有效的 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多