Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add row to BQ from a dictionary #3368

Closed
patitonav opened this issue May 4, 2017 · 3 comments
Closed

add row to BQ from a dictionary #3368

patitonav opened this issue May 4, 2017 · 3 comments
Assignees
Labels
api: bigquery Issues related to the BigQuery API. type: question Request for information or clarification. Not an issue.

Comments

@patitonav
Copy link

I am having trouble inserting a single JSON row into a BQ table from the API. I can get it done with bq-command-line or using table.upload_from_file() method. But when i am trying to do it with table.insert_data(json_dic) the method takes everything in the dictionary as a value instead of insert every value in the corresponding key of the table.
There is a way of insert a json object without passing it as a file, or declare the schema (witch is huge and can change)?

Thank you very much

@tseaver tseaver added api: bigquery Issues related to the BigQuery API. type: question Request for information or clarification. Not an issue. labels May 16, 2017
@tseaver
Copy link
Contributor

tseaver commented May 16, 2017

Table.insert_data takes as its rows argument a list of tuples. To insert a single row, you need to map your from-JSON dict onto a tuple using the table's underlying schema. E.g., something like:

table = client.table('my-table-name')
table.reload()  # fetch the table's schema
row = tuple([
    my_json_dict[field.name] for field in table.schema
])
table.insert_data([row])

@tseaver tseaver closed this as completed May 16, 2017
@tseaver
Copy link
Contributor

tseaver commented May 16, 2017

#3396 is tracking a feature request for a new convenience method to Table for this usecase.

@patitonav
Copy link
Author

row = tuple([ my_json_dict[field.name] for field in table.schema ])

This works fine for a simple dictionary but it think we need a strong solution for nested structures (a idctionary inside a dictionary)

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants