0

I'm working with an external API and the JSON error message it gives me in the response content is:

{
   "errors": {
       "items": {
          "0": {    <=== This being the index of the item that the error is in relation to
            "id": [
               {
                  "message": "invalid"
                  "code": "400"
               }
            ]
          }
       }
   }
}

How can i get the index value? I cant deserialise it and cast it to a model because the index value could be "2" or "5" or "19" but i need to get this value and because its an external API that returns this JSON I cannot change the structure of the data.

Any idea of how to achieve this using C# / .NET?

What i don't understand is how you get the key value of a nested object i guess.

Thanks

DBoi
  • 627
  • 2
  • 17
  • 35
  • This would be easy except that the key `id` looks like an array but has named properties. – Jamiec Jul 09 '20 at 10:56
  • Please beside what you have to solve, describe what you have try and did not work for you, what error do you get etc… There are many resources on how to do that Deserialize of JSON using c#, you can even make your own module to do it if no other can help you. – Aristos Jul 09 '20 at 11:03
  • @DBoi Debug it – make tests – make simple example and test it- Imaging that you have the debugger and the full program and you cannot, how anyone else can help? – Aristos Jul 09 '20 at 11:08
  • @DBoi At the end make your custom read of the json and convert it to what you look for. – Aristos Jul 09 '20 at 11:09
  • @Jamiec I've updated the JSON (as i mistakenly did not add the object), how is it easy to get the index value? – DBoi Jul 09 '20 at 11:26
  • You have a pretty standard structure of JSON now, and that can be deserialized to objects/Dictionaries/Lists as appropriate. – Jamiec Jul 09 '20 at 12:00
  • 1
    Yeah i realised its a dictionary and you can just get the key! Hate it when its the simple things that get you! Thanks for your help guys the links really pointed me in the right direction – DBoi Jul 09 '20 at 12:01

0 Answers0