Hi,
Using the search API, I get results back fine, but as it is limited to 100 results at a time, I need to loop through the next batch.
I have my results stored in JSON object data, which is formed like this
{total: 125
results:
1:
id: 5526258063
2:
id: 5526190716
… etc
100:
id: 7922329929
paging:
next:
after: 100
}
to get ‘after’, I’m trying to use:
after = data.get(“paging”, {}).get(“next”, {}).get(“after”)
But it fails!
test = data.get(“paging”,{})
RETURNS:
next:
after: 1
test2 = data.get(“paging”, {}).get(“next”, {})
returns
after: 1
but
test3 =
after = data.get(“paging”, {}).get(“next”, {}).get(“after”)
falls over with:
error: ‘str’ object has no attribute ‘copy’
Any ideas?
Phil
Hey, @PBaxter
Thanks for your question. I am not a Python expert by any means, but I know a few
— @JBeatty @Teun @ChrisoKlepke, do you have any troubleshooting tips for @PBaxter?
I did a bit of research, and it appears that your ‘data’ variable is a string at the point where you’re trying to access the ‘after’ property, instead of the dictionary-like object that you’re expecting.
I have one question — have you tried adding something like:
print(type(data))
before the line where you try to get the ‘after’ value?
This will check the type of the data object. And show whether data is still a dictionary as expected or has been converted to a string. Based on my understanding of how this should work.
If it turns out that ‘data’ is a string, the next step is to find out where it’s getting converted from an object to a string.
Best,
Jaycee
Thank you, Jaycee for looking and the advice.
In fact the data() step itself fails with the following error:
Runtime.MarshalError: Unable to marshal response: <class ‘dict’> is not JSON serializable
I get the same error with the following using json.dumps
response = requests.post(url, json=json_body, headers=headers)
newdata = response.json()
json_data = json.dumps(newdata)
The API call itself is okay. The response.json() action *looks* like it’s working but clearly the ‘after’ part is not serialized.
I read a couple of coding articles about ‘how to serialize’ JSON but they are a bit above me. Presumably there is a super-simple HubSpot-friendly solution for this somewhere?
Phil