bradchattergoon
bradchattergoon

Reputation: 482

Get JSON format for Google BigQuery Data using Pandas/Python

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

Upvotes: 0

Views: 2429

Answers (1)

user983302
user983302

Reputation: 1437

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

Upvotes: 1

Related Questions