How to deserialize this Json string in System.Text.Json

I am getting error:

An unhandled exception of type 'System.Text.Json.JsonException' occurred in System.Text.Json.dll
',' is invalid after a single JSON value. Expected end of data. Path: $ | LineNumber: 10 | BytePositionInLine: 2.

Code:

        public class InstrumentData
        {
            //[JsonInclude]
            //[JsonPropertyName("Id")] 
            public int Id { get; set; }
            //[JsonInclude]
            //[JsonPropertyName("Type")]
            public string Type { get; set; }
            //[JsonInclude]
            //[JsonPropertyName("Kind")]
            public string Kind { get; set; }
            //[JsonInclude]
            //[JsonPropertyName("Date")]
            public DateTime Date { get; set; }
            //[JsonInclude]
            //[JsonPropertyName("Status")]
            public string Status { get; set; }
            //[JsonInclude]
            //[JsonPropertyName("Value")]
            public double Value { get; set; }
            //public Object Properties { get; set; }
        }

        static void Main(string[] args)
        {
            //string jsonFile = "C:\\Users\\Piotr\\Documents\\Assets-battery\\ASSETID-2486-response_1694084415096.json";
            //string jsondata = File.ReadAllText(jsonFile);
            string jsondata =    " {                                              \n" +
                                 "   \"Id\": 4897244,                             \n" +
                                 "   \"Type\": \"InstrumentVoltage\",             \n" +
                                 "   \"Kind\": \"Value\",                         \n" +
                                 "   \"Date\": \"2023-01-06T17:40:14\",           \n" +
                                 "   \"Status\": \"Official\",                    \n" +
                                 "   \"Value\": 12.75,                            \n" +
                                 "   \"Properties\": {                            \n" +
                                 "     \"OriginalStatus\": \"Official\"           \n" +
                                 "   }                                            \n" +
                                 " },                                             \n" +
                                 " {                                              \n" +
                                 "   \"Id\": 4897245,                             \n" +
                                 "   \"Type\": \"BatteryPercent\",                \n" +
                                 "   \"Kind\": \"Value\",                         \n" +
                                 "   \"Date\": \"2023-01-06T17:40:14\",           \n" +
                                 "   \"Status\": \"Official\",                    \n" +
                                 "   \"Value\": 99,                               \n" +
                                 "   \"Properties\": {                            \n" +
                                 "     \"OriginalStatus\": \"Official\"           \n" +
                                 "   }                                            \n" +
                                 " },                                             \n";
            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                AllowTrailingCommas = true
            };


            InstrumentData data = JsonSerializer.Deserialize<InstrumentData>(jsondata, options);
        }
    }

BTW I want to skip the "Properties" part of the record.

>Solution :

There are two problems with this code:

  1. The json is not right. You have two items, so this should probably be an array. ( A set of items in json is represented as "[ item1, item2 ]". That’s commonly referred to as a json array. the items could then be literals/single values, objects or again arrays. See also: https://json.org/json-en.html )
  2. Having an array, you’ll also need to deserialize to an array (or collection).

I made a little Fiddle to show the fix:

using System;
using System.Text.Json;
                    
public class Program
{
    public static void Main()
    {
        string jsondata =    " [{                                              \n" +
                                 "   \"Id\": 4897244,                             \n" +
                                 "   \"Type\": \"InstrumentVoltage\",             \n" +
                                 "   \"Kind\": \"Value\",                         \n" +
                                 "   \"Date\": \"2023-01-06T17:40:14\",           \n" +
                                 "   \"Status\": \"Official\",                    \n" +
                                 "   \"Value\": 12.75,                            \n" +
                                 "   \"Properties\": {                            \n" +
                                 "     \"OriginalStatus\": \"Official\"           \n" +
                                 "   }                                            \n" +
                                 " },                                             \n" +
                                 " {                                              \n" +
                                 "   \"Id\": 4897245,                             \n" +
                                 "   \"Type\": \"BatteryPercent\",                \n" +
                                 "   \"Kind\": \"Value\",                         \n" +
                                 "   \"Date\": \"2023-01-06T17:40:14\",           \n" +
                                 "   \"Status\": \"Official\",                    \n" +
                                 "   \"Value\": 99,                               \n" +
                                 "   \"Properties\": {                            \n" +
                                 "     \"OriginalStatus\": \"Official\"           \n" +
                                 "   }                                            \n" +
                                 " }]";  
        var result = JsonSerializer.Deserialize<InstrumentData[]>(jsondata);
        result.Dump();
    }
}

public class InstrumentData
        {
            
            public int Id { get; set; }
            
            public string Type { get; set; }
            
            public string Kind { get; set; }
            
            public DateTime Date { get; set; }
            
            public string Status { get; set; }
            
            public double Value { get; set; }
            
        }

In Action: https://dotnetfiddle.net/2YeTJw

You also did have a trailing comma. That can also be a problem with the default settings, but you can allow it providing serializer options. See: https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.allowtrailingcommas?view=net-7.0#system-text-json-jsonserializeroptions-allowtrailingcommas

Leave a Reply