I am using python client library files for listing cloud assets. I need to move that output in to 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`
Kindly help it out. Do i need to use any other library files to convert to csv??
CodePudding user response:
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, seeassets.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'sassets
property (list ofAsset
) instead.