Serializing GoogleAdsRow in C#

30 views
Skip to first unread message

axtens

unread,
Apr 13, 2022, 3:23:17 AM4/13/22
to Google Ads API and AdWords API Forum
Having built up a list of GoogleAdsRow into
```
         var result = new List<GoogleAdsRow>();
```
how can I now get a list of all the fields in each row so as to build up TSV report, with headings at the top and the columnar data below.

-- Bruce

Zweitze

unread,
Apr 13, 2022, 9:22:42 AM4/13/22
to Google Ads API and AdWords API Forum
When you request a report, you'll reserve a response object with a few members, among that "Results" which is a list of returned GoogleAdsRows.
Among the other members you will find "FieldMask" which contains a list of names of the requested fields.

With the FieldMask list, you can determine which fields you should include in that report you want to make.

axtens

unread,
Apr 13, 2022, 9:57:51 AM4/13/22
to Google Ads API and AdWords API Forum
So I've been able to retrieve the fields using FieldMask. Is it possible to programmatically access the fields in the GoogleAdsRow? For example, from the "Immediate Window" in VS2019 where result contains a list of GoogleAdsRows:
```
result[0].CustomerClient.ClientCustomer
"customers/7212153394"
result[0]["CustomerClient.ClientCustomer"]
error CS0021: Cannot apply indexing with [] to an expression of type 'GoogleAdsRow'
```
How does one achieve a dynamic field selection?

Google Ads API Forum Advisor

unread,
Apr 13, 2022, 10:35:29 AM4/13/22
to bruce....@gmail.com, adwor...@googlegroups.com
Hi Axtens,

Thank you for reaching out to the Google Ads API support team.

To investigate the issue further, could you please provide the complete request and response logs, with the request-id?

If you haven't yet, logging can be enabled by navigating to the Client libraries > Your client library (ex. Java) > Logging documentation, which you can access from this link. You can provide it via Reply privately to author option. If this option is not available, then send it instead on this email address googleadsa...@google.com.

Thanks,
Google Logo
Nirmita
Google Ads API  Team
 


ref:_00D1U1174p._5004Q2a2kR1:ref

Zweitze

unread,
Apr 14, 2022, 8:27:47 AM4/14/22
to Google Ads API and AdWords API Forum
Your code shows a different problem than your description.

About your code:
Your first post shows variable "result" is a List. But in your second post you access that variable as an array.

About dynamic fields:
Look into .net reflection. It allows you to look up the value of class member, given its name.
Essentially you have an object -one of the GoogleAdsRow that is in your list- and a name -which comes from FieldMask-. With reflection you can get its value. Actually you need to do this twice. Using your last code: Assuming an instance of Google AdsRow, first you use reflection to get the property with name "CustomerClient". Store its value in a variable of type "object". Then use reflection on THAT object, and retrieve the property named "ClientCustomer".
If reflection is too slow for you (you expect 10,000s of rows), check out package FastMember, which allows the same - but much faster.

.net reflection goes way beyond the intention of this forum, you should check out Stackoverflow.

I have two remarks though:
1. When the query returns zero rows, FieldMask probably has no fields. Make sure to check it.
2. The GoogleAds package does some name wrangling, I am not sure FieldMask contains names from the raw result of the API, or contains similar wrangled names.


You may want to reconsider the idea of dynamic fields. If you only have a dozen, or dozens of report types, it's probably easier to hardcode every one of these reports.

Google Ads API Forum Advisor

unread,
Apr 18, 2022, 11:21:59 AM4/18/22
to zwe...@gmail.com, bruce....@gmail.com, adwor...@googlegroups.com
Hi Bruce,

Thank you for providing information.

With regards to your concern, you can reach out to the C# client library owner for issue.

Thank you Zweitez helping to Bruce.

Google Ads API Forum Advisor

unread,
Apr 18, 2022, 11:24:36 AM4/18/22
to zwe...@gmail.com, bruce....@gmail.com, adwor...@googlegroups.com
Hi Bruce,
 
If you any question related to the API, you can raise concern here.
Reply all
Reply to author
Forward
0 new messages