如何使用Python将bigquery返回的结果转换为Json格式?

时间:2019-04-15 00:20:38

标签: python json google-bigquery

使用Python从Bigquery公开数据集中选择数据,获取结果后需要以JSON格式打印。如何将结果转换为JSON?谢谢!

尝试过row[0],但出错了。

try:
    raw_results = query.rows[0]
    zipped_results = zip(field_names, raw_results)
    results = {x[0]: x[1] for x in zipped_results}
except IndexError:
    results = None

# from google.cloud import bigquery
# client = bigquery.Client()

query = """
    SELECT word, word_count
    FROM `bigquery-public-data.samples.shakespeare`
    WHERE corpus = @corpus
    AND word_count >= @min_word_count
    ORDER BY word_count DESC;
"""
query_params = [
    bigquery.ScalarQueryParameter("corpus", "STRING", "romeoandjuliet"),
    bigquery.ScalarQueryParameter("min_word_count", "INT64", 250),
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(
    query,
    # Location must match that of the dataset(s) referenced in the 
    query.location="US",
    job_config=job_config,
)  # API request - starts the query

# Print the results
for row in query_job:
    print("{}: \t{}".format(row.word, row.word_count))
assert query_job.state == "DONE"

3 个答案:

答案 0 :(得分:3)

目前没有自动转换的方法,但是有一种非常简单的手动方法可以转换为json:

records = [dict(row) for row in query_job]
json_obj = json.dumps(str(records))

另一种选择是使用熊猫进行转换:

df = query_job.to_dataframe()
json_obj = df.to_json(orient='records')

答案 1 :(得分:0)

实际上,您可以让BigQuery直接生成JSON。像这样更改您的查询:

query = """
SELECT TO_JSON_STRING(word, word_count) AS json
FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = @corpus
AND word_count >= @min_word_count
ORDER BY word_count DESC;
"""

现在结果将只有一个名为json的列,具有JSON格式的输出。

答案 2 :(得分:0)

在以下位置使用_row_tuple_from_json(row,schema)方法:

https://google-cloud-python.readthedocs.io/en/stable/_modules/google/cloud/bigquery/_helpers.html