0

I have a JSON file. I want to get all Guid lists from validation. I tried to get options of validation. Then iterate this list and all Guid lists using id. items.FirstOrDefault(x => x.Key == "validate").Value.Options; How to write LINQ query for this. Below is the sample JSON.

{
"validation": {
          "options": [
            "Fail",
            "Success",
            "Resume"
          ],
          "extras": {
            "Fail" : {
            "id": "892DB3B6-BE0D-49F8-995A-50E921C2B778"
            },
             "Success" : {
            "id": "BC0EDB51-8317-4AFB-915F-B0579FADB500"
            },
             "Resume" : {
            "id": "59786216-FF4A-4E5C-ACB2-1BCE0A6E8D7d"
            }
          }
        }
        
        "vm": {
          "options": [
            "vm1",
            "vm2",
            "vm3"
          ],
          "extras": {
            "vm1" : {
            "id": "433DB3B6-BE0D-49F8-9D5A-50E921C2B999"
            },
             "Success" : {
            "id": "RT0EDB51-8317-4AFB-915F-B0579FADB888"
            },
             "Resume" : {
            "id": "44C86216-FF4A-4E5C-ACB2-1BCE0A6E9675"
            }
          }
        }
    }
user768853
  • 269
  • 1
  • 4
  • 16

1 Answers1

0

First, the option list is received and then the id based on that option is received and use "Newtonsoft.Json"

string RootName = "validation";
string extrasName = "extras";
string optionsName = "options";

JObject sample = (JObject)JsonConvert.DeserializeObject(jsonString);
IJEnumerable<JToken> items = sample[RootName][optionsName].Values();


List<Guid> result = new List<Guid>();

foreach (var content in items)
{
    var typeoptions = content.Value<string>();
    string valueitem = sample[RootName][extrasName][typeoptions]["id"].Value<string>();
        result.Add(Guid.Parse(valueitem));
}

abolfazl sadeghi
  • 2,277
  • 2
  • 12
  • 20