1
1
import datetime
2
2
import logging
3
+ import json
3
4
import pickle
4
5
import random
5
6
import uuid
@@ -756,6 +757,7 @@ def copy(
756
757
allow_jagged_rows : bool = True ,
757
758
quote : Optional [str ] = None ,
758
759
schema : Optional [List [dict ]] = None ,
760
+ convert_dict_columns_to_json : bool = True ,
759
761
** load_kwargs ,
760
762
):
761
763
"""
@@ -786,6 +788,8 @@ def copy(
786
788
template_table: str
787
789
Table name to be used as the load schema. Load operation wil use the same
788
790
columns and data types as the template table.
791
+ convert_dict_columns_to_json: bool
792
+ If set to True, will convert any dict columns (which cannot by default be successfully loaded to BigQuery to JSON strings)
789
793
**load_kwargs: kwargs
790
794
Arguments to pass to the underlying load_table_from_uri call on the BigQuery
791
795
client.
@@ -812,6 +816,19 @@ def copy(
812
816
else :
813
817
csv_delimiter = ","
814
818
819
+ if convert_dict_columns_to_json :
820
+ # Convert dict columns to JSON strings
821
+ for field in tbl .get_columns_type_stats ():
822
+ if "dict" in field ["type" ]:
823
+ new_petl = tbl .table .addfield (
824
+ field ["name" ] + "_replace" , lambda row : json .dumps (row [field ["name" ]])
825
+ )
826
+ new_tbl = Table (new_petl )
827
+ new_tbl .remove_column (field ["name" ])
828
+ new_tbl .rename_column (field ["name" ] + "_replace" , field ["name" ])
829
+ new_tbl .materialize ()
830
+ tbl = new_tbl
831
+
815
832
job_config = self ._process_job_config (
816
833
job_config = job_config ,
817
834
destination_table_name = table_name ,
0 commit comments