Hi Deny,
Thanks for the post, and sorry that you're having an issue here.
Can you share a little more detail about the problem you're having? I might be able to help you get around the issue, and if not can help post a feature request so that we can fix it in the future.
It seems like you might be having an issue with one of the client libraries. Can you let me know which language you're using?
Thanks!
Ben Karl, Google Ads API Team
Hi Ben,
I’m using the python library and was very disappointed to find out that the results from the API queries are delivered as objects which are non-iterable, making the use of data dictionaries a lot more difficult and a lot less elegant.
Other Google APIs present results as dictionaries which can be inspected for content (fields and values) according to the same data dictionary that was used for the request. That makes configuring API extractions easy to build, maintain and improve, using code such as:
for element in my_api_dict:
if element[“id”] in returned_values:
results.append(returned_values[element[“id”]]
On the other hand, the new Google Ads API (which should supposedly be better than previous ones?) demands that the result set elements are named explicitly, like:
results = [returned_values.table1.column1,
returned_values.table1.column2,
returned_values.table1.column3, …
]
I feel very dirty to admit I came up with a fugly and shameful solution which is converting the results to string and then making some modifications to turn it into a yaml string and parse it, so I could transform it into a dictionary. Ugly solutions make me extremely uncomfortable and frustrated, and I don’t think I should have to do things like this in 2020, *especially* dealing with an API developed by the almighty Google.
I can live with the ugly solution if you can live with my deep disappointment in your company development teams.
Cheers,
Deny
import functools # Borrowed from: https://github.com/googleads/google-ads-python/blob/master/google/ads/google_ads/util.py#L51 def get_nested_attr(obj, attr, *args): def _getattr(obj, attr): return getattr(obj, attr, *args) return functools.reduce(_getattr, [obj] + attr.split(".")) for batch in response: fields = batch.field_mask.paths for row in batch.results: for field in fields: # field will be a str path, i.e. "campaign.id" or "metrics.average_cost" value = get_nested_attr(row, field) # you can also define logic based on the field name if "campaign.id" in field: # do something