0

I have the following Json (is a file attached to the project):

{
  "Errors": {
    "NoCountry": {
      "MSG": "The CountryCode field is required.",
      "Description": "Error encountered when Country parameter is missing from the request"
    },
    "NoLOI": {
      "MSG": "Validation failed: \r\n -- LengthOfInterview cannot be empty"
      "Description": "Error encountered when LOI parameter is missing from the request"
    }
  }
}

I need to extract the values for e.g. Errors.NoCompletes.MSG in order to use it in an assert to compare it with an output that i get from the API. Till now i tried to create a dictionary that looks like this:

public class ErrorsDictionary
{
  public string MSG;
  public string Description;
}

public class DicRoot
{
    public Dictionary<string, ErrorsDictionary> Errors { set; get; }
}

And use it like this:

DicRoot Json = JsonConvert.DeserializeObject<DicRoot>(File.ReadAllText(@"c:\users\banu_\source\repos\TestJsonLib\TestJsonLib\Json\ErrorMSG.json"));
foreach (var f in Json.Errors)
{
    Console.WriteLine("Nume={0} Mesaj={1} Description={2}",f.Key, f.Value.MSG, f.Value.Description);
}

The problem is that i cannot figure out, how can i extract a specific value, like what i said above for Errors.NoLOI.MSG, in order to be able to use it in an assert like Assert.Equals(ex, MyParam);

Svek
  • 12,350
  • 6
  • 38
  • 69
Banu
  • 21
  • 1
  • 1
  • 6

4 Answers4

1

You can also use JsonPath, anonymous types and string interpolation if you like:

JObject obj = JObject.Parse(json);

var errors = obj
    .SelectTokens("$.Errors.*")
    .ToDictionary(
        e => ((JProperty)e.Parent).Name,
        e => new { Msg = e["MSG"], Descr = e["Description"] });

foreach (var e in errors)
{
    Console.WriteLine($"Nume={e.Key} Mesaj={e.Value.Msg} Description={e.Value.Descr}");
}
Thowk
  • 386
  • 4
  • 15
0

I think what you are asking for is this?

DicRoot dict = JsonConvert.DeserializeObject<DicRoot>(File.ReadAllText("foo.json"));
string msg = dict["NoLOI"].MSG;
Svek
  • 12,350
  • 6
  • 38
  • 69
  • This is exactly what i needed, only mention is that it should be: string msg = dict.Errors["NoLOI"].MSG; – Banu Jul 01 '18 at 19:19
0

I know this looks like a bit working around. However, it's working.

class Program
{
    static void Main(string[] args)
    {
        string json = "{\"Errors\": {\"NoCountry\": {\"MSG\": \"The CountryCode field is required.\",\"Description\": \"Error encountered when Country parameter is missing from the request\"},\"NoLOI\": {\"MSG\": \"Validation failed: \r\n -- LengthOfInterview cannot be empty\", \"Description\": \"Error encountered when LOI parameter is missing from the request\"},}}";

        var Json = JsonConvert.DeserializeObject<ErrorsClass>(json);
        var obj = JsonConvert.DeserializeObject<Dictionary<string, ErrorsDictionary>>(Json.Errors.ToString());

        foreach (var f in obj)
        {
            Console.WriteLine("Nume={0} Mesaj={1} Description={2}", f.Key, f.Value.MSG, f.Value.Description);
        }
        Console.Read();
    }
}

public class ErrorsDictionary
{
    public string MSG { get; set; }
    public string Description { get; set; }
}

public class DicRoot
{
    public Dictionary<string, ErrorsDictionary> ErrorsDic { set; get; }
}

class ErrorsClass
{
    public object Errors { get; set; }
}

Output:

Nume=NoCountry Mesaj=The CountryCode field is required. Description=Error encountered when Country parameter is missing from the request Nume=NoLOI Mesaj=Validation failed: -- LengthOfInterview cannot be empty Description=Error encountered when LOI parameter is missing from the request

L_J
  • 2,351
  • 10
  • 23
  • 28
0

You can use Newtonsoft.json NuGet. Try this

var files = JObject.Parse(YourJson);
var recList = files.SelectTokens("$..Errors").ToList();
foreach (JProperty prop in recList.Children())
{
    string key = prop.Name.ToString();
    string value = prop.Value.ToString();
    //Do your stuffs here
}
Prany
  • 2,078
  • 2
  • 13
  • 31