0

When getting data from external API, I receive an object where a value is false when it is empty.

For example:

"product_id": [7800,"Bananenblad Naturel - 484"]

OR

"product_id": false
"picking_date": false

OR

"picking_date": "2023-08-11 10:10:39"

How can deserialize this kind of object?

An example of the object:

{
  "id": 1111,
  "product_id": [
    2222,
    "test 1"
  ],
  "product_qty": 1.0,
  "picking_date": false,
  "partner_id": [
    10,
    "Funeral"
  ],
  "picking_id": [
    20,
    "Testing"
  ],
  "picking_state": "cancel"
}, 
{
  "id": 2222,
  "product_id": [
    3333,
    "Primus"
  ],
  "product_qty": 1.0,
  "picking_date": "2023-08-11 10:10:39",
  "partner_id": false,
  "picking_id": false,
  "picking_state": "cancel"
}

I've tried using JSON converter options, ignore error handling with deserializer, ... different types of object types etc.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • What does it send if the value is `false`?!? – Heretic Monkey Aug 16 '23 at 15:26
  • Please see this link: https://stackoverflow.com/questions/39347858/deserialize-json-property-as-bool-or-double. This link shows how to deserialize a property value as a bool or a double. You can write your own JsonConverters to deserialize to string or bool or datetimes. – SoftwareDveloper Aug 16 '23 at 15:30
  • *"An example of the object"*: that's two objects and not valid JSON. Is it an array? – trincot Aug 16 '23 at 15:49
  • What serializer are you using Newtonsoft or Microsoft? – Serge Aug 16 '23 at 16:06
  • That JSON is shocking! Do you have any influence at all over the production of the JSON from the external API? I would be concerned writing a converter to wrangle this into an object model. The producer has not understood JSON de/serialisation and I would worry that successive versions of the API could include more nastiness and break the converter. – swatsonpicken Aug 16 '23 at 16:15
  • Assuming "id" always has a real value, i.e. is never false, what about the other elements, "product_qty", "partner_id", "picking_id", "picking_state"? Can they also take a value of false if empty? – swatsonpicken Aug 16 '23 at 16:23

1 Answers1

0

you can try this code

using Newtonsoft.Json;

    var products = JsonConvert.DeserializeObject<List<Product>>(json);

public class BoolJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JToken.Load(reader);
        if (token.Type != JTokenType.Boolean)
            if (token.Type == JTokenType.Array)
                return token.ToObject<List<object>>();
            else return token.ToObject<DateTime>();
        return null;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

class

public partial class Product
{
    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("product_id")]
    public List<object> ProductId { get; set; }

    [JsonProperty("product_qty")]
    public long ProductQty { get; set; }

    [JsonConverter(typeof(BoolJsonConverter))]
    [JsonProperty("picking_date")]
    public DateTime? PickingDate { get; set; }

    [JsonConverter(typeof(BoolJsonConverter))]
    [JsonProperty("partner_id")]
    public List<object> PartnerId { get; set; }

    [JsonConverter(typeof(BoolJsonConverter))]
    [JsonProperty("picking_id")]
    public List<object> PickingId { get; set; }

    [JsonProperty("picking_state")]
    public string PickingState { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45