skip to Main Content

I have following JSON format.

{
   "data": [
     {
        "id": 1,
        "variants": [
            {
               "name": "Test",
               "images": {
                    "WHITE": [
                         {
                           "id": 13,
                           "name": "Testname"
                         }
                      ]
                 } 
            }
           ] 
     }
     ]
}

Now I am Desalinizing it using following method :

var items = JsonConvert.DeserializeObject<dynamic>(content);

And then in items I am getting variants.

I am using dynamic because in this images node in some record it is coming as empty array and in some it is coming as JSON object so..

I have following dynamic object. If I write down following console line then below output will come.

Console.WriteLine(variant.images)

It’s output is looking like this below :

{
   "WHITE": [ // I want to get this WHITE in one variable, this color name.
       {
          "id": 13,
          "name": "Testname"
       }
    ]
}

Now this WHITE will come dynamic in few records it can come BLACK and in few it can come RED..

So based on that color value I need to put further logic. So how can I access that value only.

I want to save that color name value in one variable and then want to use it.

Thanks

2

Answers


  1. I’d avoid deserializing into dynamic, rather use JObject.Parse().

    If you know what color name to search for:

    var input = "{"data":[{"id":1,"variants":[{"name":"Test","images":{"WHITE":[{"id":13,"name":"Testname"}]}}]}]}";
    var json = JObject.Parse(input);
    var colorToSearchFor = "WHITE";
    var colorElement = json.SelectToken($"$.data[0].variants[0].images.{colorToSearchFor}[0]", true)!;
    
    Console.WriteLine(colorElement["id"]!.Value<int>());
    Console.WriteLine(colorElement["name"]!.Value<string>());
    
    // prints "13" and "Testname"
    

    If you don’t know what the color name is, but know where it should be:

    var input = "{"data":[{"id":1,"variants":[{"name":"Test","images":{"WHITE":[{"id":13,"name":"Testname"}]}}]}]}";
    var json = JObject.Parse(input);
    var imagesElement = json.SelectToken($"$.data[0].variants[0].images", true) as JObject;
    
    foreach (var possibleColorElement in imagesElement.Properties())
    {
        Console.WriteLine(possibleColorElement.Name);
    }
    
    // Prints "WHITE"
    

    My token selectors us a lot of [0]‘s, so adapt those if the object you are looking for isn’t the first object in the arrays. [*] might work, but use with care.

    Login or Signup to reply.
  2. I have showed you several times already how to use LINQ

        var jObj = JObject.Parse(json);
        
        List<string> imageColors = jObj["data"]
                          .SelectMany(x => ((JArray)x["variants"])
                          .SelectMany(x => ((JObject)x["images"])
                          .Properties().Select(x => x.Name)))
                          .ToList(); // or FirstOrDefault if you want one colour
    
        string firstImageColor = imageColors[0]; // "WHITE"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search