0

how can I check data in a JSON File in C#? I would like my program to check if the data is present in the JSON file or not with an if, how should I do? For example in this file JSON here How am I supposed to do the check? (sorry for my bad English)

AlexTrev
  • 3
  • 2
  • Check for what? – TheGeneral Jun 26 '20 at 09:00
  • take it as a string is you can't make a class object – prisar Jun 26 '20 at 09:01
  • To see if the data type is present in the file or not – AlexTrev Jun 26 '20 at 09:02
  • your data is organized as a dictionary, so read it, convert the JSON to dictionary and lookup the value in the dictionary like in this [Q/A](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – jps Jun 26 '20 at 09:06
  • Hello, and welcome to stackoverflow. Might you please [edit] your question to include your JSON as **text** rather than as a screenshot? It's policy here not to to use images for textual data, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. – dbc Jun 27 '20 at 16:19

1 Answers1

0

You can validate the JSON against schema like this.

string schemaJson = @"{
  'description': 'A person',
  'type': 'object',
  'properties':
  {
    'name': {'type':'string'},
    'hobbies': {
      'type': 'array',
      'items': {'type':'string'}
    }
  }
}";

JsonSchema schema = JsonSchema.Parse(schemaJson);

JObject person = JObject.Parse(@"{
  'name': 'James',
  'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");

bool valid = person.IsValid(schema);

Reference: https://www.newtonsoft.com/json/help/html/JsonSchema.htm

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197