0

How to skip/ignore/handle invalid json objects in Newtonsoft JSON?

Let's say we have string like:

[{
    "$id": "xc1",
    "sdfdsgds0sdfsadgdxc,sfgsagdfgdsfdm",
    "gxcgdfs"
}, {
    "$id": "2",
    "Property1": "Value",
    "Property2": "Value2"
}]

While doing

JsonConvert.DeserializeObject<T>(jsonString);

So the first object is invalid but I would like to read second valid one, but I've got JsonReaderException, is there a way to achieve ignoring/skipping invalid objects in array and go further with deserialization?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • does this answer your question https://stackoverflow.com/questions/26107656/ignore-parsing-errors-during-json-net-data-parsing – Kaj Feb 28 '20 at 12:12
  • Might be this will help you https://stackoverflow.com/questions/36576928/how-can-i-deserialize-an-invalid-json-truncated-list-of-objects – Jasmin Solanki Feb 28 '20 at 12:18
  • Does this answer your question? [Ignore parsing errors during JSON.NET data parsing](https://stackoverflow.com/questions/26107656/ignore-parsing-errors-during-json-net-data-parsing) – Selim Yildiz Feb 28 '20 at 12:29
  • First approach bubble the problem as well for collection so the whole collection is null. –  Feb 28 '20 at 13:12
  • 1
    It's not easy when the JSON itself is **malformed** (which it is in your case), rather than just invalid for the current deserialization target. `JsonTextReader` is a state machine that maintains the current token type and a set of valid transitions, and if the JSON token stream does not conform to the [JSON standard](https://www.json.org/json-en.html) the parser will not know what to expect next. (In your case there is a property name but no value). Continuing onward becomes problematic in such situations. – dbc Feb 28 '20 at 15:40
  • Hello @dbc is that you? What have you meant in last comment? https://github.com/JamesNK/Newtonsoft.Json/issues/1580 –  Feb 28 '20 at 16:04
  • In that case the JSON is well-formed but not valid for the model being deserialized. As such, it's possible to create a `JsonConverter` that screens out invalid values. In your case the JSON is malformed (upload it to https://jsonlint.com/ to confirm) so a converter can't help. – dbc Feb 28 '20 at 16:57

1 Answers1

1

I have use JsonSerializerSettings class for determine errors while getting serialize JSON string.

var settings = new JsonSerializerSettings
{
    Error = (obj, args) =>
    {
        var contextErrors = args.ErrorContext;
        contextErrors.Handled = true;
    }
};
var result = streamReader.ReadToEnd();
List<ViewModel> viewModel = JsonConvert.DeserializeObject<List<viewModel>>(result, settings);
  • This will set the collection to null, it won't get a second item. –  Feb 28 '20 at 13:23
  • You have make view model for constant JSON object. In my case my model is public class ViewModel { public string Property1 { get; set; } public string Property2 { get; set; } } If mentioned properties was not found, then that properties will be excluded. – Pandurang Choudekar Feb 28 '20 at 13:57
  • The object won't be even created. First object will throw 2 errors one for invalid object second for collection, but I don't see the way to continue deserializing. –  Feb 28 '20 at 14:09