'SimplePublicObjectWithAssociations' object has no attribute 'values'

I’m trying to create an analytics pipeline in Python / Jupyter. I have been able to access contacts and deals data using api_client.crm.contacts.get_all() which provides me wit a JSON Class ‘List’. If I copy and paste this output and use json_normalize, I am able to convert it into a dataframe. However if I attempt to access the data directly I get the following error: ‘SimplePublicObjectWithAssociations’ object has no attribute ‘values’ or cannot unpack non-iterable SimplePublicObjectWithAssociations object. Every attempt I’ve made to convert SimplePublicObjectWithAssociations has failed. Further I have been able to access single records by their ID, suggesting that something on the Hubspot side is not allowing me to loop over the contacts data to pull it all into Jupyter. Has anyone else encountered this issue?

code:
all_contacts = api_client.crm.contacts.get_all()

data = all_contacts

df = pd.json_normalize(data, max_level=2)

‘SimplePublicObjectWithAssociations’ object has no attribute ‘values’
data = all_contacts
for i,x in data:
data[i]= x.to_dict()
df = json.loads(data, max_level=2)

cannot unpack non-iterable SimplePublicObjectWithAssociations object

Hey, @JRourke :waving_hand: Thanks for your question.

While I’m no expert in Python, I know a few — :waving_hand: @JBeatty @ChrisoKlepke @Klapprodt :waving_hand:
I have a few questions that can help the community be better informed about your project:

  • Did you try converting the ‘SimplePublicObjectWithAssociations’ objects to dictionaries using the to_dict() method before passing them to pd.json_normalize?
  • Can you clarify what you’re trying to achieve with your second loop?

Thank you! — Jaycee

Hi @JRourke ,

I think there is a step missing. Try this:

all_contacts = api_client.crm.contacts.get_all()

contacts_list = []

for contact in all_contacts:
contacts_list.append(contact.to_dict())

contacts_df = pd.json_normalize(contacts_list, max_level=2)

contacts_df.head(5)

----

If you want to get a single contact by ID, try this:

contact = api_client.crm.contacts.basic_api.get_by_id('112358')

Cheers,
Marcus

Thank you Marcus!
This method converts it into a pandas.core.frame.Pandas, which can then be turned into a data frame using the following “pd.DataFrame(contacts_df)”.
Appreciate your input!

Happy to help :+1: