JSON格式错误

5

在尝试将此JSON导入到Google BigQuery表时,我遇到了以下错误:

file-00000000: JSON表格遇到太多错误,放弃。行数:1;错误数:1。(错误代码:无效)

文件file-00000000中的行以位置0开始的JSON解析错误。在没有对象开始的情况下遇到数组的开头。(错误代码:无效)

这是JSON内容:

[{'instrument_token': 11192834, 'average_price': 8463.45, 'last_price': 8471.1, 'last_quantity': 75, 'buy_quantity': 1065150, 'volume': 5545950, 'depth': {'buy': [{'price': 8471.1, 'quantity': 300, 'orders': 131072}, {'price': 8471.0, 'quantity': 300, 'orders': 65536}, {'price': 8470.95, 'quantity': 150, 'orders': 65536}, {'price': 8470.85, 'quantity': 75, 'orders': 65536}, {'price': 8470.7, 'quantity': 225, 'orders': 65536}], 'sell': [{'price': 8471.5, 'quantity': 150, 'orders': 131072}, {'price': 8471.55, 'quantity': 375, 'orders': 327680}, {'price': 8471.8, 'quantity': 1050, 'orders': 65536}, {'price': 8472.0, 'quantity': 1050, 'orders': 327680}, {'price': 8472.1, 'quantity': 150, 'orders': 65536}]}, 'ohlc': {'high': 8484.1, 'close': 8336.45, 'low': 8422.35, 'open': 8432.75}, 'mode': 'quote', 'sell_quantity': 998475, 'tradeable': True, 'change': 1.6151959167271395}]

http://jsonformatter.org/ 也会对这个 JSON 块进行解析错误。需要帮助理解格式有何问题——这是一个来自 Rest API 的 JSON。


正如@user94559所解释的那样,首先将json数据转换为有效格式。然后将其转换为ndjson并上传。它可以工作!您可以使用在线工具将json转换为ndjson。 - Avinash
2个回答

11

这不是有效的JSON。 JSON使用双引号而不是单引号。 此外,True 应该是 true

如果我猜的话,我会猜测这是Python代码被传递为JSON。 :-)

我怀疑即使将其转换为正确的JSON格式,它也不是Google BigQuery期望的格式。从https://cloud.google.com/bigquery/data-formats#json_format中可以看出,您应该有一个文本文件,每行一个JSON对象。尝试只执行此操作:

{"mode": "quote", "tradeable": true, "last_quantity": 75, "buy_quantity": 1065150, "depth": {"buy": [{"quantity": 300, "orders": 131072, "price": 8471.1}, {"quantity": 300, "orders": 65536, "price": 8471.0}, {"quantity": 150, "orders": 65536, "price": 8470.95}, {"quantity": 75, "orders": 65536, "price": 8470.85}, {"quantity": 225, "orders": 65536, "price": 8470.7}], "sell": [{"quantity": 150, "orders": 131072, "price": 8471.5}, {"quantity": 375, "orders": 327680, "price": 8471.55}, {"quantity": 1050, "orders": 65536, "price": 8471.8}, {"quantity": 1050, "orders": 327680, "price": 8472.0}, {"quantity": 150, "orders": 65536, "price": 8472.1}]}, "change": 1.6151959167271395, "average_price": 8463.45, "ohlc": {"close": 8336.45, "high": 8484.1, "open": 8432.75, "low": 8422.35}, "instrument_token": 11192834, "last_price": 8471.1, "sell_quantity": 998475, "volume": 5545950}

JSONformatter.org即使使用双引号也会出现错误。 - Chandan Kumar
@ChandanKumar:但从根本上说,答案是:“那不是JSON”,这是你自己可以轻松确定的事情。不要手工制作 JSON,而是在你使用的任何语言/环境中构建一个结构,并使用适当的 JSON 序列化程序创建 JSON。 - T.J. Crowder
1
更新:这个 JSON 在 JSONformatter.org 中显示为有效(单引号被双引号替换,True 被替换为 "True"),但 Google BigQuery 仍然会给出相同的错误。 - Chandan Kumar
1
@smarx 谢谢。这是数据供应商伪装成 JSON 的 Python 代码 :). 原始的 JSON 也是行分隔格式(开头和结尾在同一行)。使用您发布的 JSON 仍然会出现相同的错误。 - Chandan Kumar
@smarx 现在正在工作。谢谢 :). Google Bigquery 抛出了一个错误,但数据已经更新到表中。 - Chandan Kumar
显示剩余2条评论

6

原始数据是一个有效的JSON记录,但这并不适用于Biq Query,原因如下:

  1. Google Big Query 支持一行一个JSON对象 {}。请查看此处了解更多信息。
  2. 这意味着您不能提供列表 [] 作为JSON记录并期望Big Query能够检测到它。您必须始终每行一个JSON对象。

  3. 以下是我所说的内容的快速参考:

(Image courtesy: json.org) 还有更多。


最后,

我强烈建议您阅读下面的内容,并查看链接了解有关不同形式的JSON结构的更多信息,请从json.org中阅读。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接