How to convert Google-Ads API GoogleAdsRow to json?

2,711 views
Skip to first unread message

Alexander Belozerov

unread,
Apr 1, 2019, 3:12:38 PM4/1/19
to AdWords API and Google Ads API Forum
Hello there,
Im querying google ads api and need to save results as json. What is the best way to convert GoogleAdsRow type into json?


The result of the google ads api call is GoogleAdsRow that looks like this:

campaign {
  resource_name
: "customers/321/campaigns/123"
  id
{
    value
: 123
 
}
  name
{
    value
: "Campaign Name A"
 
}
}
metrics
{
  clicks
{
    value
: 711
 
}
  impressions
{
    value
: 2662
 
}
}
segments
{
  device
: DESKTOP
  ad_network_type
: SEARCH
  date
{
    value
: "2019-03-01"
 
}
}

Instead of campaign, it can be adgroup, keywords, etc.
What is the best way to convert this kind of data to json?

googleadsapi...@google.com

unread,
Apr 1, 2019, 5:15:57 PM4/1/19
to AdWords API and Google Ads API Forum
Hello Alexander,

You can refer this APi call example mentioned here, which will give the response in JSON format, when you make API call using CURL operations. For example you could try the below CURL operation to get details of a campaign.

CURL example to get Campaigns:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN"      -H "developer-token: DEVELOPER_TOKEN"      -H "Content-Type: application/json"      https://googleads.googleapis.com/v1/customers/<CUSTOMER_ID>/googleAds:search      --data '{query: "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"}'

Please let me know if you have any further questions.

Regards,
Sai Teja, Google Ads API Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Was your question answered? Please rate your experience with us by taking a short survey.
If not -- reply to this email and tell us what else we can do to help.

Take Survey

Also find us on our blog and discussion group:
http://googleadsdeveloper.blogspot.com/search/label/adwords_api
https://developers.google.com/adwords/api/community/

Rahul Kumar

unread,
Jun 11, 2019, 6:54:07 AM6/11/19
to AdWords API and Google Ads API Forum
Hi Sai,

I think Alexander is asking about the SDK. I am also facing the same problem. If someone is using the python SDK for google ads API, how can one convert the GoogleAdsRow to json. I have tried to use a CustomEncoder by subclassing the json.JSONEncoder, but that is not the correct way as I will never be sure if there is any particular data type that I am missing. Is there a method like toJson() for this?

Thanks,
Rahul

Google Ads API Forum Advisor Prod

unread,
Jun 11, 2019, 3:54:42 PM6/11/19
to adwor...@googlegroups.com
Hello Rahul,

We don't have such functionality to get JSON format directly via GoogleAdsRow. Hence, I provided the CURL operation which will give JSON as output. I recommend you to post your concern on the client library issue tracker , Our client library owners are better equipped to answer this.

Regards,
Sai Teja, Google Ads API Team

ref:_00D1U1174p._5001UBmf8H:ref

UK Analytics

unread,
Jun 12, 2019, 5:24:21 PM6/12/19
to AdWords API and Google Ads API Forum

from google.protobuf import json_format

### code to get the object ###

rec_obj
= row.recommendation.text_ad_recommendation.ad
json_str
= json_format.MessageToJson(rec_obj)
print(json_str)


Adam

Mark Edmondson

unread,
Dec 3, 2019, 10:14:26 AM12/3/19
to AdWords API and Google Ads API Forum
I couldn't get the protobuf to work but using the SOAP seralizer zeep this seems to work:

from zeep import helpers
def toJSON(x):
input_dict = helpers.serialize_object(x)
return json.dumps(input_dict)

Google Ads API Forum Advisor Prod

unread,
Dec 4, 2019, 12:15:16 PM12/4/19
to ma...@iihnordic.com, adwor...@googlegroups.com

Hello,

Thank you for reaching out. If you are using the our client libraries, it is not possible to retrieve the data in JSON format. However, as an alternative you can use the cURL operation to get the output in JSON format. You can refer to this guide for more details on how to interact with Google Ads API using cURL. Please give this a try and let me know if you have any further questions.

Regards,
Nikisha Patel, Google Ads API Team



ref:_00D1U1174p._5001UODiuH:ref

Thomas Koller

unread,
Jul 17, 2020, 10:58:01 AM7/17/20
to AdWords API and Google Ads API Forum
Unfortunately doesn't work in Ads API v4 (beta):

TypeError: Object of type GoogleAdsRow is not JSON serializable

Google Ads API Forum Advisor Prod

unread,
Jul 20, 2020, 9:30:55 AM7/20/20
to kol...@exito.de, adwor...@googlegroups.com
Hi Thomas,

Thank you for reaching out. I just want to confirm that you're following this example when making the cURL request to get the JSON response. If so, please share your client customer ID, cURL request, and any additional error details via Reply privately to author so I can further investigate the issue.

Regards,
Mitchell

Google Ads API Team

ref:_00D1U1174p._5004Q21msBS:ref

Michael Birk

unread,
Jul 22, 2020, 3:52:00 PM7/22/20
to AdWords API and Google Ads API Forum
Hi Rahul,

I was able to use this Python library to convert the Ads API v3 responses to JSON:


mcb

kol...@exito.de

unread,
Sep 8, 2020, 9:03:16 AM9/8/20
to AdWords API and Google Ads API Forum
We are using the Python client library because we don't want to write all the basic stuff.

In the meantime I found out how you can directly convert the API result to a pandas dataframe in Python:

from pandas import json_normalize
from google.protobuf.json_format import MessageToJson
...

results_report = ga_service.search(account_id, query=query_report, page_size=_DEFAULT_PAGE_SIZE)

list_all_rows = []

for row in results_report:  
    serialized_json = MessageToJson(row).replace('\n', '')    
    serialized_json_dict = json.loads(serialized_json)
    
    df_row = json_normalize(serialized_json_dict)  
    list_all_rows.append(df_row)

if len(list_all_rows) == 0:
    continue

df_all_rows = pd.concat(list_all_rows)


This works fine in Ads API v4 and v5. The same code works for 5 or 50 columns.
TBH: I can't understand why you don't provide such a compact transformation from the API result to a dataframe in your library examples.

Who wants to retrieve every single field like this (taken from the examples)?  That's awkward and tedious  ;-)

for row in results:
     ad_group = row.ad_group
     ad_group_criterion = row.ad_group_criterion
     keyword = row.ad_group_criterion.keyword  
     ... 

rahul...@decision-tree.com

unread,
May 18, 2021, 7:36:40 AM5/18/21
to AdWords API and Google Ads API Forum
I have found a working solution. This will go something like this:
```
import proto 
response = ga_service.search_stream(search_request) 
for batch in response: 
      for row in batch.results: 
               logging.debug(proto.Message.to_dict(row))
```
Reply all
Reply to author
Forward
0 new messages