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

fix: make unicode characters working well in load_table_from_json #865

Merged
2 changes: 1 addition & 1 deletion google/cloud/bigquery/client.py
Expand Up @@ -2762,7 +2762,7 @@ def load_table_from_json(

destination = _table_arg_to_table_ref(destination, default_project=self.project)

data_str = "\n".join(json.dumps(item) for item in json_rows)
data_str = "\n".join(json.dumps(item, ensure_ascii=False) for item in json_rows)
encoded_str = data_str.encode()
data_file = io.BytesIO(encoded_str)
return self.load_table_from_file(
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_client.py
Expand Up @@ -7775,6 +7775,42 @@ def test_load_table_from_json_w_invalid_job_config(self):
err_msg = str(exc.value)
assert "Expected an instance of LoadJobConfig" in err_msg

def test_load_table_from_json_unicode_emoji_data_case(self):
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES

client = self._make_client()

emoji = "\U0001F3E6"
json_row = {"emoji": emoji}
json_rows = [json_row]

load_patch = mock.patch(
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
)

with load_patch as load_table_from_file:
client.load_table_from_json(json_rows, self.TABLE_REF)

load_table_from_file.assert_called_once_with(
client,
mock.ANY,
self.TABLE_REF,
size=mock.ANY,
num_retries=_DEFAULT_NUM_RETRIES,
job_id=mock.ANY,
job_id_prefix=None,
location=client.location,
project=client.project,
job_config=mock.ANY,
timeout=None,
)

sent_data_file = load_table_from_file.mock_calls[0][1][1]

# make sure json_row's unicode characters are only encoded one time
expected_bytes = b'{"emoji": "' + emoji.encode("utf8") + b'"}'
assert sent_data_file.getvalue() == expected_bytes

# Low-level tests

@classmethod
Expand Down