Search code examples
c#posthubspot

Encode JSON with custom property names


I want to make a POST request to the Hubspot API (https://developers.hubspot.com/docs/methods/contacts/create_or_update) and I need the JSON to match this format:

{ "properties": [ { "property": "firstname", "value": "HubSpot" } ] }

Instead im getting this:

{ "properties": [ { "Key": "email", "Value": "[email protected]" }, { "Key": "firstname", "Value": "testfirstname" } ] }

Instead of "property" and "value" , my code generates "Key" and "Value" , how can I change my JSON to match the right format?

Here is how I generate that dictionary:

    public class HubspotContact
    {
        public Dictionary<string, string> properties { get; set; }
    }

    private static readonly HttpClient client = new HttpClient();

    class DictionaryAsArrayResolver : DefaultContractResolver
    {
        protected override JsonContract CreateContract(Type objectType)
        {
            if (objectType.GetInterfaces().Any(i => i == typeof(IDictionary) ||
               (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))))
            {
                return base.CreateArrayContract(objectType);
            }

            return base.CreateContract(objectType);
        }
    }

And this is how I generate the JSON:

HubspotContact foo = new HubspotContact();
foo.properties = new Dictionary<string, string>();
foo.properties.Add("email", "[email protected]");
foo.properties.Add("firstname", "firstname");

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new DictionaryAsArrayResolver();

string json = JsonConvert.SerializeObject(foo, settings);

Finally this is how I send my request:

    var httpWebRequest =(HttpWebRequest)WebRequest.Create("https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/[email protected]/?hapikey=myapikey");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())){
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            Console.WriteLine(result.ToString());
        }

Right now with the request as is I get this error:

System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'

Solution

  • Change HubspotContact to this:

    class PropertyValue
    {
        public string Property { get;set;}
        public string Value { get;set;}
    }
    class HubspotContact
    {
        public List<PropertyValue> Properties {get;set;}
    }
    

    It should serialize to a proper format and it does not need custom serializer.