I have an ASP.NET application that uses the Azure AD Graphics API. Often, when an incorrect operation is performed with the Graph API, an exception is thrown.
The following code shows an invalid call to the Graph API, which will throw an exception:
var userToUpdate = await activeDirectoryClient.Users.GetByObjectId("user@domain.net").ExecuteAsync();
userToUpdate.GivenName = "";
try
{
await userToUpdate.UpdateAsync();
}
catch (Exception e)
{
}
The internal exception message is a JSON string with forward slashes in front of each quotation mark. It looks something like this:
"{\"odata.error\":{\"code\":\"Request_BadRequest\",\"message\":{\"lang\":\"en\",\"value\":\"Invalid value specified for property 'givenName' of resource 'User'.\"},\"values\":[{\"item\":\"PropertyName\",\"value\":\"givenName\"},{\"item\":\"PropertyErrorCode\",\"value\":\"InvalidValue\"}]}}"
Attaching a screenshot of the Locals window in which an exception message is detected:

I would like to convert this JSON to a .NET object to return detailed error information. I use the JSON.NET library for this, and I assume that JSON is deserialized into an object ODataError
:
var error = Newtonsoft.Json.JsonConvert.DeserializeObject<ODataError>(e.InnerException.Message);
null
, , .
, JSON? , ?