Checking key name in nested json

I need to check if a key name exists in a json file, but I cannot figure out how.

Json file:

{
  "value": [
    {
      "from": 1430611201000,
      "to": 1430697600000,
      "ref": "2015-05-03",
      "value": "8.4",
      "quality": "Y"
    }
  ],
  "updated": 1430726400000,
  "parameter": {
    "key": "2",
    "name": "Lufttemperatur",
    "summary": "medelvärde 1 dygn, 1 gång/dygn, kl 00",
    "unit": "degree celsius"
  },

      

I think this is the closest to me, but it will always be false (even if I search for an existing key): content is a JObject.

var searchDate = content.Properties().Select(p => p.Value).Children().Any(p=>p.Contains("quality"));

      

EDIT: I changed the name of the key to "quality" to clarify the question. I will also use another json file that contains the "date" key instead of "from" and "to". To split the files I want to know if "date" exists or not.

+3


source to share


2 answers


If the property being checked always belongs to an element in the property value

, you can do it like this:

var isQualityExists = 
            content["value"].Any(v => ((JObject)v).Properties()
                                                  .Any(p => p.Name.Contains("quality"))
                                );

      



Otherwise, you can create a function that recursively checks for the existence of a specific property name across the entire tag JObject

.

+3


source


date doesn't exist in json. This is why you are wrong as mentioned in one of the comments. If you want to find the key: "to"



if("to" in json_object) 
{
//your logic goes here
}

      

0


source







All Articles