0

I am trying input some validation in C#. I have a LIST of JSON Object, and I want to add a validation that will check for a value of certain key and make sure they are all same in all the JSON object in the array.

LIST -

subtitles = [{ frameRate : '25', dropFrame: False}, { frameRate : '25', dropFrame: False}, { frameRate : '25', dropFrame: False}, { frameRate : '23', dropFrame: False}]

I want to add validation that will loop through every object and make sure that the value of the key frameRate is same through the array.

This is what i tried to do - I created a aempty list and then tried to push all the frameRates and then looped through it and compared the value with previous one.

List<string> subtitleSegmentsFrameRates = new List<string>();

            foreach (var subtitle in segmentSubtitles) 
            {
                subtitleSegmentsFrameRates.Add(subtitle.frameRate);
                    
            }

            for (int i = 0; i < subtitleSegmentsFrameRates.Count; i++) {
                if (i != 0) {
                    if (subtitleSegmentsFrameRates[i] != subtitleSegmentsFrameRates[i - 1]) {
                        throw new CustomException("Frame Rates arocss segments do not match.");
                    }
                }
            }

Is there a better way to do this ?

curious_debugger
  • 299
  • 2
  • 8
  • 23
  • Have you tried [deserializing to an anonymous object](https://stackoverflow.com/questions/6904825/deserialize-json-to-anonymous-object)? – D M Jan 22 '21 at 15:28
  • 4
    Deserialize it and use `.Distinct(s => s.FrameRate).Count() == 1`. – ProgrammingLlama Jan 22 '21 at 15:28
  • 3
    JSON is a string, not a data structure. You need to parse that string into a data structure and query the data. You can use LINQ or a loop for brute-force searches, or use a container like Dictionary, HashSet, etc to accelerate specific queries – Panagiotis Kanavos Jan 22 '21 at 15:28
  • 1
    You can deserialize them using `Newtonsoft.Json` or other Json library and use loop, linq to iterate through all items and filter out what you need. – mrogal.ski Jan 22 '21 at 15:29
  • What have you tried so far? Where did you stuck? – Peter Csala Jan 22 '21 at 15:44
  • What I did was to create a list and then push all the frame rates into the list and then tried to compare each of the element with previous one. – curious_debugger Jan 22 '21 at 15:51
  • I meant it on the code level. What have you written so far? – Peter Csala Jan 22 '21 at 15:53
  • Edited the question, can you take a look at my approach ? – curious_debugger Jan 22 '21 at 16:00
  • 1
    You could start your for loop from 1 instead of 0. You could also combine the for and foreach loops to avoid the population of the subtitleFragmentsFrameRates – Peter Csala Jan 22 '21 at 16:10

2 Answers2

1

First of all convert your json string into JArray and use .All() to check you have same value for frameRate property of all elements.

using System;
using Newtonsoft.Json.Linq;
using System.Linq;

...
var validFrameRate = JArray.Parse(subtitles)  //Convert an Sub titles to JArray
           .All(x => x.Value<string>("frameRate") == "25");  //Check all frame rates are same or not.

Console.WriteLine(validFrameRate ? "Same frameRate in all elements" : "Different FrameRate in all elements");

.Net Fiddle


JArray is an array of JToken, to get actual value you need to convert JToken to its actual type. Here we used .Value<string>() to get value of frameRate property of JToken

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
1

You can compare all the items against the first one after parsing input to JArray and linq All method

var jArray = JArray.Parse(subtitles);
bool allSame = jArray.All(j => j["frameRate"].Value<string>() == jArray[0]["frameRate"].Value<string>());

j["frameRate"] is type of JToken that's why to convert to string .Value<string>() is invoked over it.

Another way which is also suggested by @John:

bool same = jArray.Select(j => j["frameRate"].Value<string>()).Distinct().Count() == 1

Check this fiddle - https://dotnetfiddle.net/5xOJB5

user1672994
  • 10,509
  • 1
  • 19
  • 32
  • 2
    `Value` would be enough. You don't need to call `ToObject`. [Reference](https://stackoverflow.com/questions/58188277/difference-between-jtoken-toobjectt-vs-jtoken-valuet) – Peter Csala Jan 22 '21 at 15:43