0
votes

I am trying to query Google BigQuery using the Pandas/Python client interface. I am following the tutorial here: https://cloud.google.com/bigquery/docs/bigquery-storage-python-pandas. I was able to get it to work but I want to query the data as the JSON format that can be downloaded directly from the WebUI (see screenshot). Is there a way to download data as the JSON structure pictured instead of converting it to the data frame object?

I imagine the command would be somewhere around this part of the code from the tutorial:

dataframe = (
    bqclient.query(query_string)
    .result()
    .to_dataframe(bqstorage_client=bqstorageclient)
)

enter image description here

1
Not exactly what you are asking for, but converting DataFrame to JSON is also trivial pandas.pydata.org/pandas-docs/stable/reference/api/…Yun Zhang

1 Answers

1
votes

Just add .to_json(orient='records') call after converting to dataframe:

json_data = bqclient.query(query_string).result().to_dataframe(bqstorage_client=bqstorageclient).to_json(orient='records')

pandas docs