-
Notifications
You must be signed in to change notification settings - Fork 308
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
Provide json.dumps kwargs to load_table_from_json #1564
Comments
I am not sure I am following what the ask is in this issue. Please elaborate on
|
The problem is when passing a dictionary object containing a A viable workaround is to convert each To avoid this problem altogether, we can provide additional arguments to Below is a basic example illustrating the issue and the successful workaround: import datetime as dt
from google.cloud.bigquery import Client, LoadJobConfig, SchemaField
project_id = "PROJECT_ID"
client = Client(project=project_id)
table_schema = [SchemaField("datetime", "DATETIME"), SchemaField('date', "DATE")]
job_config = LoadJobConfig(write_disposition="WRITE_APPEND", schema=table_schema)
table_id = f"{project_id}.test.test"
# Successful insertion using isoformat strings
data = [
{
"datetime": dt.datetime(2012, 11, 1, 12, 9, 10).isoformat(), # Using isoformat() to convert datetime to string
"date": dt.date(2020, 11, 1).isoformat(), # Using isoformat() to convert date to string
}
]
job = client.load_table_from_json(data, table_id, job_config=job_config)
job.result() However, if we simply pass the data = [
{
"datetime": dt.datetime(2012, 11, 1, 12, 9, 10), # datetime object without isoformat conversion
"date": dt.date(2020, 11, 1), # date object
}
]
job = client.load_table_from_json(data, table_id, job_config=job_config)
job.result() We get the following error: Traceback (most recent call last):
File "/example.py", line 23, in <module>
job = client.load_table_from_json(data, table_id, job_config=job_config)
File "/lib/python3.9/site-packages/google/cloud/bigquery/client.py", line 2845, in load_table_from_json
data_str = "\n".join(json.dumps(item, ensure_ascii=False) for item in json_rows)
File "lib/python3.9/site-packages/google/cloud/bigquery/client.py", line 2845, in <genexpr>
data_str = "\n".join(json.dumps(item, ensure_ascii=False) for item in json_rows)
File "lib/python3.9/json/__init__.py", line 234, in dumps
return cls(
File "lib/python3.9/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "lib/python3.9/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "lib/python3.9/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable Setup:
|
Thanks @HCA97, if you are interested in loading json with non-string data types such as As to adding json.dumps kwargs, I think a better option is to use the same group of conversion functions in If |
Hi, we are currently facing problems submitting json objects with a date field in them. It would make sense to provide custom json.dumps
kwargs
to load_table_from_json in python-bigquery/google/cloud/bigquery/client.py. We could then providedefault
parameter to the json.dumps withinload_table_from_json
.The text was updated successfully, but these errors were encountered: