Skip to content

Issue in the Spanner Python client when an array value is present at the top-level of a JSON column #716

@omarqouqas

Description

@omarqouqas

The Python Spanner client does not support deserialising a JSON column which contains an array as the top level element. Although the insertion works (when pre-serialising using json.loads), reading back the row fails, either by returning incorrect data, or by throwing an error.

What is the business impact you are facing:

Unability to deploy a new feature in production relying on array values as top-level elements in JSON columns.

Environment details

  • Python version: python3.8
  • google-cloud-spanner version: Python client google-cloud-spanner 3.13.0

Description of the workload (business logic, access pattern, use case):

Accessing JSON columns using the Python client fails

Steps to reproduce

  1. https://github.com/flovouin/spanner-python-json-column#running-the-example
  2. ValueError: dictionary update sequence element #0 has length 1; 2 is required
    JsonObject([{"myFirstObject": 1}])

Code example

https://github.com/flovouin/spanner-python-json-column

Stack trace

File "venv/lib/python3.8/site-packages/google/cloud/spanner_v1/streamed.py", line 145, in __iter__
    self._consume_next()
  File "venv/lib/python3.8/site-packages/google/cloud/spanner_v1/streamed.py", line 137, in _consume_next
    self._merge_values(values)
  File "venv/lib/python3.8/site-packages/google/cloud/spanner_v1/streamed.py", line 105, in _merge_values
    self._current_row.append(_parse_value_pb(value, field_types[index]))
  File "venv/lib/python3.8/site-packages/google/cloud/spanner_v1/_helpers.py", line 244, in _parse_value_pb
    return JsonObject.from_str(value_pb.string_value)
  File "venv/lib/python3.8/site-packages/google/cloud/spanner_v1/data_types.py", line 45, in from_str
    return cls(json.loads(str_repr))
  File "venv/lib/python3.8/site-packages/google/cloud/spanner_v1/data_types.py", line 30, in __init__
    super(JsonObject, self).__init__(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the googleapis/python-spanner API.priority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions