Search code examples
c#asp.net-corejson-patch

asp.net - How to get / deserialize object of JsonPatchDocument?


I could serialize the JsonPatchDocument model by using JsonConvert.SerializeObject(), but the type of result is string, how can I convert it to normal array type? Or how to get JsonPatchDocument object straight to array?

var pathSerialized = JsonConvert.SerializeObject(patch);
Console.WriteLine(pathSerialized);

// Result as string: 
// "[{"value":"2018-08-30","path":"/openTo","op":"replace"},{"value":"2018-04-01","path":"/openFrom","op":"replace"}]" 

Solution

  • You don't have to serialize the JsonPatchDocument object at all. You can access its properties directly through the object. For example filtering for the path property:

    var elementsWithPath = patch.Operations.Where(o => o.path.Equals("some path"));