在将json数组插入到bigQuery时出现错误

时间:2019-01-03 10:51:49

标签: google-bigquery

  

读取数据时出错,错误消息:无法解析JSON:启动新数组时找不到对象。 BeginArray返回false

我在下面的示例json数据中创建了。这是json的数组。而且每个json对象都位于换行符中。

当我只加载一个json对象而不保留在数组内部时,它起作用了。

JSON正文-

[
  { "item_name": "dfkhjf", "gtin": "123456", "brand": "Om-Publication","category_name": "books", "country_code": "IN", "marktet_place": "india", "price": 2239, "sellerId": 234, "create_time": "2017-07-19T16:00:46.000Z" },
  { "item_name": "toy-gun", "gtin": "1234234445", "brand": "Toy", "category_name": "toy", "country_code": "IN", "marktet_place": "flipMe", "price": 2239, "sellerId": 234, "create_time": "2017-08-19T16:00:46.000Z" },
  { "item_name": "Drone", "gtin": "12342356456", "brand": "Drone-XX", "category_name": "drone", "country_code": "IN", "marktet_place": "drone-maker", "price": 2239, "sellerId": 234, "create_time": "2017-09-19T16:00:46.000Z" }
]

1 个答案:

答案 0 :(得分:1)

loading JSON data stored in GCS to BigQuery的文档中所述,JSON数据必须位于Newline Delimited JSON format中,其中每一行都是有效的独立JSON值,因此,您应使用(2)代替(1) )):

(1)

[
  { "item_name": "dfkhjf", "gtin": "123456", "brand": "Om-Publication","category_name": "books", "country_code": "IN", "marktet_place": "india", "price": 2239, "sellerId": 234, "create_time": "2017-07-19T16:00:46.000Z" },
  { "item_name": "toy-gun", "gtin": "1234234445", "brand": "Toy", "category_name": "toy", "country_code": "IN", "marktet_place": "flipMe", "price": 2239, "sellerId": 234, "create_time": "2017-08-19T16:00:46.000Z" },
  { "item_name": "Drone", "gtin": "12342356456", "brand": "Drone-XX", "category_name": "drone", "country_code": "IN", "marktet_place": "drone-maker", "price": 2239, "sellerId": 234, "create_time": "2017-09-19T16:00:46.000Z" }
]

(2)

{ "item_name": "dfkhjf", "gtin": "123456", "brand": "Om-Publication","category_name": "books", "country_code": "IN", "marktet_place": "india", "price": 2239, "sellerId": 234, "create_time": "2017-07-19T16:00:46.000Z" }
{ "item_name": "toy-gun", "gtin": "1234234445", "brand": "Toy", "category_name": "toy", "country_code": "IN", "marktet_place": "flipMe", "price": 2239, "sellerId": 234, "create_time": "2017-08-19T16:00:46.000Z" }
{ "item_name": "Drone", "gtin": "12342356456", "brand": "Drone-XX", "category_name": "drone", "country_code": "IN", "marktet_place": "drone-maker", "price": 2239, "sellerId": 234, "create_time": "2017-09-19T16:00:46.000Z" }

更新:

这里有逐步的指南,以显示其工作原理:

使用共享的内容创建一个JSON文件(以我的情况为file.json)(确保删除每行末尾的数组括号[]和逗号,) :

$ cat file.json
{ "item_name": "dfkhjf", "gtin": "123456", "brand": "Om-Publication","category_name": "books", "country_code": "IN", "marktet_place": "india", "price": 2239, "sellerId": 234, "create_time": "2017-07-19T16:00:46.000Z" }
{ "item_name": "toy-gun", "gtin": "1234234445", "brand": "Toy", "category_name": "toy", "country_code": "IN", "marktet_place": "flipMe", "price": 2239, "sellerId": 234, "create_time": "2017-08-19T16:00:46.000Z" }
{ "item_name": "Drone", "gtin": "12342356456", "brand": "Drone-XX", "category_name": "drone", "country_code": "IN", "marktet_place": "drone-maker", "price": 2239, "sellerId": 234, "create_time": "2017-09-19T16:00:46.000Z" }

运行以下命令将文件加载到BQ:

$ bq load --autodetect --source_format=NEWLINE_DELIMITED_JSON dataset.table file.json                                   
Upload complete.
Waiting on bqjob_XXXXXXXXXXX ... (1s) Current status: DONE

现在查询表以检查内容是否正确上传:

$ bq query --use_legacy_sql=false "SELECT * FROM dataset.table"
Waiting on bqjob_r3ef14ac0d0a6c856_000001681819e9fc_1 ... (0s) Current status: DONE
+----------+-------+---------------------+---------------+----------------+--------------+---------------+-------------+-----------+
| sellerId | price |     create_time     | marktet_place |     brand      | country_code | category_name |    gtin     | item_name |
+----------+-------+---------------------+---------------+----------------+--------------+---------------+-------------+-----------+
|      234 |  2239 | 2017-07-19 16:00:46 | india         | Om-Publication | IN           | books         |      123456 | dfkhjf    |
|      234 |  2239 | 2017-08-19 16:00:46 | flipMe        | Toy            | IN           | toy           |  1234234445 | toy-gun   |
|      234 |  2239 | 2017-09-19 16:00:46 | drone-maker   | Drone-XX       | IN           | drone         | 12342356456 | Drone     |
+----------+-------+---------------------+---------------+----------------+--------------+---------------+-------------+-----------+