'Unable to Process JSON Hubspot API Call
I will try to provide as much information as possible. I have been trying for hours to solve this, but I have not been able to which is why I am here. Please help me.
I am trying to create a new Company Property on Hubspot. This is the the code that I am using:
foreach (CompanyPropertyResponse prop in companyStagingdata)
{
var founditems = Testhubspotproperties.Find(i => i.name == prop.name);
if (founditems == null)
{
//runner.LogInformational($"We need property Group: {group.name}");
//Create propery group
var createcompanypropertyurl = "https://api.hubapi.com/properties/v1/companies/properties?hapikey=" + HubspotService.DestinationAPIKey;
var createpropertyforcompany = new CompanyPropertyResponse
{
name = prop.name,
label = prop.label,
description = prop.description,
groupName = prop.groupName,
type = prop.type,
fieldType = prop.fieldType
};
if (prop.options.Count > 0)
{
createpropertyforcompany.options = new List<Option>();
foreach (var option in prop.options)
{
createpropertyforcompany.options.Add(new Option
{
label = option.label,
value = option.value
});
}
}
if (prop.type == "enumeration" && prop.options.Count == 0)
{
createpropertyforcompany.type = "string";
createpropertyforcompany.fieldType = "text";
}
string JsonCheck = JsonConvert.SerializeObject(createpropertyforcompany).ToString();
if (!JsonHelper.IsValidJson(JsonCheck))
{
continue;
}
try
{
WebRequest postcompanyproperty_webRequest = Helper.GetWebRequest(createcompanypropertyurl, JsonCheck, "POST");
postcompanyproperty_webRequest.GetResponse();
}
catch (Exception e)
{
//runner.LogError(e, JsonConvert.SerializeObject(createpost));
throw;
}
//runner.LogInformational($"Company Group Created: {group.name}");
}
}
I am comparing an already existing Hubspot account and I want to synchronize one account to the other.
If a property does not exist in the destination account, then create it from the source account.
As you can see, I am trying to check if the JSON is valid by parsing it through the method JsonHelper.IsvalidJson:
public static bool IsValidJson(string strInput)
{
try
{
var obj = JObject.Parse(strInput);
return true;
}
catch
{
return false;
}
}
However, I still reach an exception on this part of the code:
try
{
WebRequest postcompanyproperty_webRequest = Helper.GetWebRequest(createcompanypropertyurl, JsonCheck, "POST");
postcompanyproperty_webRequest.GetResponse();
}
catch (Exception e)
{
//runner.LogError(e, JsonConvert.SerializeObject(createpost));
throw;
}
I have been looking at the API POST and GET records that show on Hubspot and these are the errors: Source Account:
{
"status": "error",
"message": "Invalid input JSON on line 1, column 42: Unexpected character (',' (code 44)): expected a value",
"correlationId": "9934949d-e73a-4881-b83e-e791098b45ad"
}
Destination account:
{
"status": "error",
"message": "Unable to process JSON",
"correlationId": "7e07b8ad-ae51-4ac0-894c-88b0893fcc6d"
}
Finally, this is the JSON that gets passed in when the code tries to POST the new property:
{
"name": "activation_timestamp",
"label": "Activation Timestamp",
"description": "Date Fusebill logged account activation",
"groupName": "account_details",
"type": "date",
"fieldType": "date",
"fieldLevelPermission": null,
"hidden": false,
"updatedAt": null,
"referencedObjectType": null,
"optionSortStrategy": null,
"createdUserId": null,
"externalOptionsReferenceType": null,
"searchableInGlobalSearch": false,
"hasUniqueValue": false,
"numberDisplayHint": null,
"readOnlyDefinition": false,
"externalOptions": false,
"textDisplayHint": null,
"displayOrder": 0,
"isCustomizedDefault": false,
"formField": false,
"readOnlyValue": false,
"mutableDefinitionNotDeletable": false,
"favorited": false,
"favoritedOrder": 0,
"calculated": false,
"displayMode": null,
"showCurrencySymbol": null,
"optionsAreMutable": null,
"searchTextAnalysisMode": null,
"createdAt": null,
"hubspotDefined": null,
"currencyPropertyName": null,
"updatedUserId": null,
"deleted": null,
"options": null
}
Solution 1:[1]
I found the solution. My JSON values which should have been passed as null were passed in as the string "null" and Hubspot didn't like that.
I used the following while serializing my Json so that it would ignore all null values.
var json = JsonConvert.SerializeObject(
objectToSerialize,
new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex Martinez |
