APIs & Integrations

dornan1
Participant

Unable to import data into Hubspot via Python 3.7

Hello, I have spent much time trying to import data to HubSpot via Python 3.7.  I was successful at importing the csv via the HubSpot UI.  However, I keep getting a 400 request with "unable to process json" comment.  I created the custom properties "policycount_SW" and "GWP_SW" within Hubspot already.

 

post_url = f'https://api.hubapi.com/crm/v3/imports?hapikey={API_key}'
payload = {
"name": "my_import",
"files": [
{
"fileName": "SW_HS_Metrics1.csv",
"fileFormat":"CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": False,
"columnName": "Company ID",
"propertyName": "COMPANY ID",
"idColumnType": "HUBSPOT_COMPANY_ID",
"columnObjectType": "COMPANY",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "Name",
"propertyName": "Name",
"columnObjectType": "COMPANY",
"foreignKeyType": "null",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "policycount_SW",
"propertyName": "policycount_SW",
"columnObjectType": "COMPANY",
"foreignKeyType": "null",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "GWP_SW",
"propertyName": "GWP_SW",
"columnObjectType": "COMPANY",
"foreignKeyType": "null",
"associationIdentifierColumn": False
}
]
}
}
]
}
files = {
'importRequest': (None, json.dumps(payload), 'application/json'),
'files': open('C:/Users/Documents/SW_HS_Metrics1.csv', 'rb')
}
res = requests.post(post_url, files=files)
print(res)
print(res.text)

0 Upvotes
5 Replies 5
dennisedson
HubSpot Product Team
HubSpot Product Team

Unable to import data into Hubspot via Python 3.7

Hey @dornan1 !

Thanks for submitting this question to the community.  

I saw this thread that might be of help to you asked by @JakeBreaksStuff . 

 

@JakeBreaksStuff , would you be able to help out here? 

@taran42 , now I know that you are a Python all star, I am going to start asking you for help 🙂 .  

 

Thanks all!

 

 

 

0 Upvotes
taran42
Contributor

Unable to import data into Hubspot via Python 3.7

@dennisedson I wouldn't say I'm an Expert just yet. 😉

 

@dornan1 In looking at your JSON, it appears as though you have an extra closing bracket } at the end. Also JSON likes for your literal values to be lowercase (the True and False). Try this: 

 

post_url = f'https://api.hubapi.com/crm/v3/imports?hapikey={API_key}'
payload = {
"name": "my_import",
"files":[
   {
      "fileName":"SW_HS_Metrics1.csv",
      "fileFormat":"CSV",
      "fileImportPage":{
         "hasHeader":true,
         "columnMappings":[
            {
               "ignored":false,
               "columnName":"Company ID",
               "propertyName":"COMPANY ID",
               "idColumnType":"HUBSPOT_COMPANY_ID",
               "columnObjectType":"COMPANY",
               "associationIdentifierColumn":false
            },
            {
               "ignored":false,
               "columnName":"Name",
               "propertyName":"Name",
               "columnObjectType":"COMPANY",
               "foreignKeyType":"null",
               "associationIdentifierColumn":false
            },
            {
               "ignored":false,
               "columnName":"policycount_SW",
               "propertyName":"policycount_SW",
               "columnObjectType":"COMPANY",
               "foreignKeyType":"null",
               "associationIdentifierColumn":false
            },
            {
               "ignored":false,
               "columnName":"GWP_SW",
               "propertyName":"GWP_SW",
               "columnObjectType":"COMPANY",
               "foreignKeyType":"null",
               "associationIdentifierColumn":false
            }
         ]
      }
   }
]
files = {
'importRequest': (None, json.dumps(payload), 'application/json'),
'files': open('C:/Users/Documents/SW_HS_Metrics1.csv', 'rb')
}
res = requests.post(post_url, files=files)
print(res)
print(res.text)

 

It's also good to run your JSON through a validator. I like to use https://jsonformatter.curiousconcept.com/

dornan1
Participant

Unable to import data into Hubspot via Python 3.7

Hi @dennisedson and @taran42,  thank you for your suggestions.  Unfortunately, I'm still getting a 'bad request' return with a "some property names are invalid."  However, these names are the same (case/spelling) as in Hubspot.  I have imported the same file after initially being unsuccessful.  I switched the Name/Company ID fields so that ID was first. 

However, this is the Python error I'm getting:

 

{"status":"error","message":"some property names are invalid: {ObjectTypeId{legacyObjectType=COMPANY}=[Company Domain, claimcount_SW, COMPANY ID, Name]}","correlationId":"3da5f8a9-5d5a-44ae-9740-5837a981f78a"}

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Unable to import data into Hubspot via Python 3.7

hey @dornan1 , 

Apologies for just now seeing this.  Are you still struggling with getting this to work?

 

I was looking at the property names and noticed that you have spaces that would typically be replaced by an underscore on HubSpot.  

 

Example -- 

 "propertyName":"COMPANY ID",

 

If this is still an issue, go ahead and send me the Portal ID and I will investigate further. 

 

Thanks!

 

 

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Unable to import data into Hubspot via Python 3.7

@taran42 , great advice with the json validation resource!  

 

Thanks again!

d

0 Upvotes