GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Easy way to unmarshall/dump objects to Json? · Issue #298 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

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

Copy link
Copy link
Closed
#299
Closed
Easy way to unmarshall/dump objects to Json?#298
#299
Copy link
Assignees
HemangChothani
Labels
api: bigqueryIssues related to the googleapis/python-bigquery API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@romainr
romainr
opened on Oct 5, 2020
Issue body actions

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, ...}

Activity

  1. product-auto-label
    added
    api: bigqueryIssues related to the googleapis/python-bigquery API.
    on Oct 5, 2020
  2. tswast commented on Oct 5, 2020

    @tswast
    tswast
    on Oct 5, 2020
    Contributor
    More actions

    2 options for you:

    (1) Most objects have a to_api_repr method, which produces a JSON-serilizable value for the class. https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.job.QueryJobConfig.html#google.cloud.bigquery.job.QueryJobConfig.to_api_repr

    (2) It's not a public method, but most objects in this library have a hidden _properties property, which is JSON-serializable. You can try access that.

  3. tswast commented on Oct 5, 2020

    @tswast
    tswast
    on Oct 5, 2020
    Contributor
    More actions

    Looks like TableListItem does not have a to_api_repr method. I'll leave this open as a feature request to add that method there.

    https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.table.TableListItem.html#google.cloud.bigquery.table.TableListItem

  4. tswast
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Oct 5, 2020
  5. romainr commented on Oct 6, 2020

    @romainr
    romainr
    on Oct 6, 2020
    Author
    More actions

    I see, this is helpful, thanks a lot! I will use _properties in the meantime.

  6. HemangChothani
    self-assigned this
    on Oct 6, 2020
  7. HemangChothani
    closed this as completedin #299on Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @HemangChothani
    HemangChothani

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

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.