Skip to content

Easy way to unmarshall/dump objects to Json? #298

@romainr

Description

@romainr

Sorry if I missed it, but could not find it in the documentation or after playing around.

Basically I am trying to get the raw json dump of objects like Tables or TableList, but via the API we are getting many composed objects that need to be unmarshalled manually one by one.

For example: what would be a way to dump into json a TableList? Thanks!

from google.cloud import bigquery
import json

client = bigquery.Client()                
                
for d in client.list_datasets():
  for t in client.list_tables(d.reference):
    print(t)
    print(repr(t))
    print(dir(t))
    print(t.table_id)
    print(json.dumps(t))
<google.cloud.bigquery.table.TableListItem object at 0x7f02c30b0760>
<google.cloud.bigquery.table.TableListItem object at 0x7f02c30b0760>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_properties', 'clustering_fields', 'created', 'dataset_id', 'expires', 'friendly_name', 'from_string', 'full_table_id', 'labels', 'partition_expiration', 'partitioning_type', 'project', 'reference', 'table_id', 'table_type', 'time_partitioning', 'to_bqstorage', 'view_use_legacy_sql']
arr
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type TableListItem is not JSON serializable
    

-->

{"created":"2020-10-05T01:43:40.077Z","dataset_id":"d","description":null, ...}

Metadata

Metadata

Labels

api: bigqueryIssues related to the googleapis/python-bigquery API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions