Hi all, am working with NewtonSoft Json.Net in a ASP.net webforms (C#) application. Please bear with me, I am very new to the Hubspot API, and JSON api in general.
My deals class (Object) looks like this, and I can GET all my deals with no issue using this mapping:
public class Deal
{
public int portalId { get; set; }
public int dealId { get; set; }
public bool isDeleted { get; set; }
public Associations associations { get; set; }
[JsonProperty(“properties”)]
public Dictionary<string, PropertyValues> properties { get; set; }
public List imports { get; set; }
public List stateChanges { get; set; }
}
public class PropertyValues
{
public string value { get; set; }
public long timestamp { get; set; }
public string source { get; set; }
public string sourceId { get; set; }
public List versions { get; set; }
}
public class DealList
{
public List<Deal> deals { get; set; }
public bool hasMore { get; set; }
public int offset { get; set; }
}
However I cannot CREATE a deal without getting the following 400 response:
{
“status”: “error”,
“message”: "Invalid input JSON on line 1, column 1: Can not deserialize instance of com.hubspot.deals.base.DealView out of START_ARRAY token",
…
}
The json string I am passing in my POST is:
{
“portalId”:0,“dealId”:0,“isDeleted”:false,
“associations”:{“associatedCompanyIds”:[124286060],“associatedVids”:[1009151],“associatedDealIds”:null},
“properties”:
{“dealname”:{“value”:“Deal Created by Hubspot API”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“hubspot_owner_id”:{“value”:“7209703”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“pipeline”:{“value”:“bb901f63-4781-471c-b955-2d0f8c0ea029”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“dealstage”:{“value”:“eb6e035b-ba94-44b5-a18b-2450cb5a690f”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“createdate”:{“value”:“1509979230000”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“part_number”:{“value”:“ABC123PARTNUMBER”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“manufacturer”:{“value”:“INTEL”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“target_price”:{“value”:“1.50”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“description”:{“value”:“Created via Hubspot API”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“amount”:{“value”:“60000”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null},
“dealtype”:{“value”:“newbusiness”,“timestamp”:0,“source”:null,“sourceId”:null,“versions”:null}
},
“imports”:null,
“stateChanges”:null
}
I’ve been researching the issue, seems the API doesn’t like trying to convert this single object (i.e. Deal) or some property of it to an ArrayList, but as of yet, I have not been able to work around the issue. I am probably doing something noobish, but have been banging my head at it to no avail. Any help would be appreciated.
I created my deal structure based on output from http://json2csharp.com which recognizes the “properties” as Dictionary<string,object> type for properties. Using this object I can deserialze all Deal GETS just fine.
However, since POST requires not a Dictionary<string, object>, instead it wants a Dictionary<string,string>, thus I can’t re-use this object to POST.
I suppose I could create a separate class to separate POST and GET operations, but that does not sound like a good idea. I imagine there is a much better way to handle this that I am not savvy to.
Any ideas / recommendations on how to resolve this?
Final reply in case this helps any other .Net integrators.
What’s I’ve opted to do is keep my Deal Object with properties in the Dictionary<string,Object> format, as that most closely matches the structure of a Deal when I POST to retrieve them This allows me to get existing deals, and they deserialize nicely into my .Net object.
For POSTing a new deal, I don’t use the formal Deal object, instead a dynamic ExpandoObject, which allows me to format my object to match the POST requirements for Creating a new Deal in the Hubspot Api. (i.e. properties can be a Dictionary<string,string>).
dynamic d = new ExpandoObject();
d.properties = new List<Property>();
d.properties.Add(new Property { name = "dealname", value = dealName });
Then, with JSON.net I can serialize this into the required json string format and POST.
Not quite as clean as a common class to represent both scenarios, but works fine for me. If anyone has any other solutions, I’d be interested to know.