I am trying to post data to an API but I am getting an error saying, "Malformed JSON string". I constructed my object based on the payload which I was given, which has been tested from python and is working fine.
What is it that am doing wrong based on my code below?
Original JSON payload which I have been given and I need to build from it.
{'i_customer': 1, 'custom_fields_values': [{'i_custom_field': 1, 'db_value': '2'}]}
My code attempt:
CustomField custom = new CustomField()
{
CustomerId = customerId,
CustomFieldsValues = new CustomFieldsValues()
{
CustomFieldId = _portaClient._CustomFieldId,
DbValue = customFieldId
}
};
var json = JsonConvert.SerializeObject(custom);
string url = string.Format(endpoint);
var client = new RestClient(url)
{
Timeout = -1
};
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("params", "{\"information\":" json "\"}");
--
var response = client.Execute(request);
The above code produces this payload:
{"i_customer":1,"custom_fields_values":{"i_custom_field":1,"db_value":"2"}}
API error response:
Error: "
{"faultcode":"malformed_json_string","faultstring":"Malformed JSON string"}"
This is my model
public class CustomField
{
[JsonProperty(PropertyName = "i_customer")]
public int? CustomerId { get; set; }
[JsonProperty(PropertyName = "custom_fields_values")]
public CustomFieldsValues CustomFieldsValues { get; set; }
}
public class UpdateCustomerCustomFieldsValuesResponse
{
[JsonProperty(PropertyName = "i_customer")]
public int? CustomerId { get; set; }
}
public class CustomFieldsValues
{
[JsonProperty(PropertyName = "i_custom_field")]
public int CustomFieldId { get; set; }
[JsonProperty(PropertyName = "db_value")]
public string DbValue { get; set; }
}
How can I fix this?
CodePudding user response:
FWIW, I believe the error is not accurate. "Malformed" has a very specific meaning that does not apply here, in that your result is well-formed and valid JSON data. This valid JSON result isn't structured in a way their system would like, which is still an error... just not the same kind of error as sending "malformed" JSON.
To fix this things, this CustomField
class:
public class CustomField
{
[JsonProperty(PropertyName = "i_customer")]
public int? CustomerId { get; set; }
[JsonProperty(PropertyName = "custom_fields_values")]
public CustomFieldsValues CustomFieldsValues { get; set; }
}
needs the CustomFieldsValues
property to show as an array or list:
public class CustomField
{
[JsonProperty(PropertyName = "i_customer")]
public int? CustomerId { get; set; }
[JsonProperty(PropertyName = "custom_fields_values")]
public List<CustomFieldsValues> CustomFieldsValues { get; set; }
}
And then, of course, you must also update any code which populates this object accordingly:
CustomField custom = new CustomField() {
CustomerId = customerId,
CustomFieldsValues = new List<CustomFieldsValues> {
new CustomFieldsValues() {
CustomFieldId = _portaClient._CustomFieldId,
DbValue = customFieldId
}
}
};
Finally, if they really do want single-quotes for field names, they've got a lot of nerve telling you your JSON is malformed, when in fact it is their own system using malformed and non-standard JSON.