Search code examples
pythoncsvgoogle-cloud-platformgoogle-cloud-asset-inventory

Object of type Asset is not JSON serializable


I am using GCP's Python client APIs for listing cloud assets. I need to move that output to a CSV file. But I can't because it is showing

TypeError: Object of type Asset is not JSON serializable

My code

response = client.list_assets(
  request={
    "parent": project_resource,
    "read_time": None,
    "asset_types": ["compute.googleapis.com/Instance"],
    "content_type": asset_v1.ContentType.RESOURCE,
    "page_size": 50,
  }
)

for asset in response:
   print(asset)
df = json_normalize(asset)
df.to_csv('list.csv', sep=',', encoding='utf-8')`

My output

TypeError: Object of type Asset is not JSON serializable

Do I need to use any other library files to convert to CSV?


Solution

  • I suspect Asset is a protocol buffer message and (these classes are) not JSON serializable.

    NOTE Confirmed that Asset is a protocol buffer. The method uses gRPC Transcoding, see assets.list

    You should (!) be able to use MessageToJSON in google.protobuf.json_format to convert the protobuf message into JSON that you can then convert to CSV. The module also includes MessageToDict which may (!?) be preferable in this case.

    Update

    Apparently (!) Google has changed its Protobuf support with API Client Libraries and uses Proto Plus for Python. I did not know this until your question. The solution is now (!):

    for asset in resp:
        j = asset_v1.Asset.to_json(asset)
    

    And, IIUC, because you need to to_json the protocol buffer messages, you will need to iterate over resp, to_json each (!?) asset and then reassemble them before converting to CSV.

    NOTE Your code appears (!?) to create each Asset in the response as a separate CSV file (list.csv) when, I suspect you really want to serialize the response's assets property (list of Asset) instead.