I am pulling a list of my contacts via the API with Python. The JSON file that is being put by Hubspot is incorrectly formatted. When I run it through Pandas in Python to convert it to a CSV, I keep getting a ValueError. Below is a sample of what I get from the API.
{
"results":[
{
"id":"101",
"properties":{
"createdate":"2020-06-05T15:18:37.746Z",
"email":"someone@aplace.com",
"firstname":"First",
"hs_object_id":"101",
"lastmodifieddate":"2020-08-12T15:17:35.104Z",
"lastname":"Last"
},
"createdAt":"2020-06-05T15:18:37.746Z",
"updatedAt":"2020-08-12T15:17:35.104Z",
"archived":false
}
],
"paging":{
"next":{
"after":"452",
"link":"https://api.hubapi.com/sampleurl.com"
},
"prev":null
}
}
As it turns out, if I incase the last section of the JSON file in {} and move the ] to the second line from the end, the file reads properly. See the code below for a sample that works properly after two, slight modifications.
{
"results": [
{
"id": "101",
"properties": {
"createdate": "2020-06-05T15:18:37.746Z",
"email": "someone@aplace.com",
"firstname": "First",
"hs_object_id": "101",
"lastmodifieddate": "2020-08-12T15:17:35.104Z",
"lastname": "Last"
},
"createdAt": "2020-06-05T15:18:37.746Z",
"updatedAt": "2020-08-12T15:17:35.104Z",
"archived": false
},
{
"paging": {
"next": {
"after": "452",
"link": "https://api.hubapi.com/sampleurl.com"
},
"prev": null
}
}
]
}
Is there a way to correct the JSON code that is coming from the API? Or perhaps I am not pulling the code properly (I am using a simple GET from Python). See the code below.
import sys
import requests
url = "https://api.hubapi.com/crm/v3/objects/contacts"
querystring = {"limit":"10","paginateAssociations":"false","archived":"false","hapikey":"API Key"}
headers = {'accept': 'application/json'}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
original_stdout = sys.stdout
with open('contacts.json', 'w') as f:
sys.stdout = f
print(response.text)
sys.stdout = original_stdout
I feel like I’m missing something obvious here.