Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

can't extract output of the "resourses" to csv. #345

Closed
testgithub-trial opened this issue Dec 16, 2021 · 1 comment
Closed

can't extract output of the "resourses" to csv. #345

testgithub-trial opened this issue Dec 16, 2021 · 1 comment
Assignees
Labels
api: cloudasset Issues related to the googleapis/python-asset API. type: question Request for information or clarification. Not an issue.

Comments

@testgithub-trial
Copy link

testgithub-trial commented Dec 16, 2021

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??

@product-auto-label product-auto-label bot added the api: cloudasset Issues related to the googleapis/python-asset API. label Dec 16, 2021
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Dec 17, 2021
@parthea parthea added type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. labels Dec 17, 2021
@parthea parthea self-assigned this Dec 17, 2021
@parthea
Copy link
Contributor

parthea commented Dec 17, 2021

Hi @testgithub-trial,

Please can you try the following which uses to_dict from proto? Please see the Inheritance section here and related discussion here.

from google.cloud import asset_v1
from pandas import json_normalize
import proto

project_resource = "projects/{}".format(<your project id>)
client = asset_v1.AssetServiceClient()

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

serializable_assets = [proto.Message.to_dict(asset) for asset in response]
df = json_normalize(serializable_assets)
df.to_csv('list.csv', sep=',', encoding='utf-8')

I'm going to close this issue but please feel free to re-open it if you have additional questions.

@parthea parthea closed this as completed Dec 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: cloudasset Issues related to the googleapis/python-asset API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants