1

I need to iterate the nested json and need to extract the few fixed attribute values from the final json object. In the below JSON, I need to do some internal logic for the title/ref and other attributes. How can I parse the below json and get the Keys like type,employeeFirstName,employeeLastName and address

JSON:

"properties": {
    "type": {
      "description": "Defines type of object being represented",
      "type": "string",
      "enum": [
        "employee"
      ]
    },
    "employeeFirstName": {
      "title": "First Name",
      "description": "Employee first name",
      "type": "string"
    },
    "employeeLastName": {
      "title": "Last Name",
      "description": "Employee Last name",
      "type": "string"
    },
    "address": {
      "title": "Address",
      "description": "Employee present address ",
      "$ref": "#/definitions/address"
        }
}

Code:

        var jsonObject = JObject.Parse(jsonFileContent);

        var propertyContent = JObject.Parse(jsonObject["properties"].ToString());
        var definitionContent = JObject.Parse(jsonObject["definitions"].ToString());

        for (int i = 0; i < propertyContent.Count; i++)
        {
            // Here I need to pass the value dynamically.
            //For Example, first iteration, type needs to be passed,
            //in second iteration, employeeFirstName needs to be passed and so on
            var tempContent = propertyContent["address"].ToString();

            var def = JObject.Parse(tempContent);

            for (int j = 0; j < def.Count; j++)
            {
                //Some working logic is added based on the specific key

            }
        }

In the above code, I need to pass the dynamic key in the step var tempContent = propertyContent["address"].ToString(); for each iteration .So, Is there any way to capture all the Keys from the propertyContent

Subburaj
  • 2,294
  • 3
  • 20
  • 37

1 Answers1

1

Here's a quick and dirty example:

using Newtonsoft.Json.Linq;
using System;

public class Program
{
    public static void Main()
    {
        {
            string sb = "           {\"properties\": {" +
                "    \"type\": {" +
                "      \"description\": \"Defines type of object being represented\"," +
                "      \"type\": \"string\"," +
                "      \"enum\": [" +
                "        \"employee\"" +
                "      ]" +
                "    }," +
                "    \"employeeFirstName\": {" +
                "      \"title\": \"First Name\"," +
                "      \"description\": \"Employee first name\"," +
                "      \"type\": \"string\"" +
                "    }," +
                "    \"employeeLastName\": {" +
                "      \"title\": \"Last Name\"," +
                "      \"description\": \"Employee Last name\"," +
                "      \"type\": \"string\"" +
                "    }," +
                "    \"address\": {" +
                "      \"title\": \"Address\"," +
                "      \"description\": \"Employee present address \"," +
                "      \"$ref\": \"#/definitions/address\"" +
                "        }" +
                "}}";
            var jsonObject = JObject.Parse(sb);

            var props = jsonObject.GetValue("properties");

            foreach (var prop in props.Values<JProperty>())
            {
                switch (prop.Name)
                {
                    case "type":
                    Console.WriteLine("Handling type: " + prop.Value);
                    break;
                    case "employeeFirstName":
                    Console.WriteLine("employeeFirstName: " + prop.Value);
                    break;
                }
            }
        }
    }
}

See the dotnetfiddle here.

Blue
  • 22,608
  • 7
  • 62
  • 92