Abhishek K Suresh
Abhishek K Suresh

Reputation: 53

Checking whether a unique data is present in a json file

I need to check whether a word is present in a JSON file or not. So if I'm searching for "root", then even though the word "byroots" contain root, it should give me false. Here's my code

using (StreamReader r = new StreamReader("filename.json"))
{
    string json1 = r.ReadToEnd();
    if (json1.Contains("root"))
    {
        filename = path + @"" + branch + "-" + testsuite.Title + ".json";
    }
}

I've also tried this condition:-

if (json1.IndexOf(testsuite.Title, StringComparison.OrdinalIgnoreCase) >= 0)

But I'm getting the same results.

Here's the json data

{
"LV": {
    "build_number": "20180517.1",
    "blah_blah": "blah",
    "name": "byroots",
    }
}

Upvotes: 1

Views: 76

Answers (1)

You should use Regex

var pattern = @"*root*";
Regex rgx = new Regex(pattern);
using (StreamReader r = new StreamReader("filename.json"))
{
 string json1 = r.ReadToEnd();
 if (rgx.IsMatch(json1))
 {
    filename = path + @"" + branch + "-" + testsuite.Title + ".json";
 }
}

Upvotes: 1

Related Questions