-1

    {
  "ErrorNumber": 10,
  "Type": "ValidationException",
  "Message": "A validation exception occurred",
  "Elements": [
    {
      "ContactID": "00000000-0000-0000-0000-000000000000",
      "Name": "Test AB",
      "EmailAddress": "abc@gmail.com",
      "Addresses": [
        {
          "AddressType": "POBOX",
          "ValidationErrors": []
        }
      ],
      "Phones": [],
      "ContactGroups": [],
      "IsCustomer": true,
      "ContactPersons": [],
      "HasValidationErrors": true,
      "ValidationErrors": [
        {
          "Message": "The contact name Test AB is already assigned to another contact. The contact name must be unique across all active contacts."
        }
      ]
    }
  ]
}

Tried to deserialize the object.

var x = ((Xero.NetStandard.OAuth2.Client.ApiException)ex.InnerException).ErrorContent;
                dynamic parsedObject = JsonConvert.DeserializeObject(x);

I have a dynamic Json string which is coming from Xero API exception. How can I access the value "ValidationErrors" and the Message?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Shereen
  • 119
  • 1
  • 17
  • Can you provide the programming language you are using and what did you try so far? – luckongas Jan 07 '21 at 02:44
  • C# code. dynamic parsedObject = JsonConvert.DeserializeObject(x); but I cannot seems to get access to array inside array – Shereen Jan 07 '21 at 02:45
  • Does this answer your question? [how to access JSON object in C#](https://stackoverflow.com/questions/16459155/how-to-access-json-object-in-c-sharp) – luckongas Jan 07 '21 at 02:49
  • I have tried it but its not working – Shereen Jan 07 '21 at 02:56
  • Could you add your code snippet to the question? – luckongas Jan 07 '21 at 03:00
  • 1
    `var result = JObject.Parse(error)["Elements"][0]["ValidationErrors"][0]` However your question is not very clear...You could deserialize this properly, though who knows what funky format they have. – TheGeneral Jan 07 '21 at 03:00

1 Answers1

1

Using json.net you can just navigate the graph with string indexers

var errors = JObject.Parse(json)["Elements"][0]["ValidationErrors"];

foreach (var error in errors)
   Console.WriteLine(error["Message"]);

Output

The contact name Test AB is already assigned to another contact. The contact name must be unique across all active contacts.
TheGeneral
  • 79,002
  • 9
  • 103
  • 141