1

I have a nested json format like given below,I need to read all the nodes till the last node to get the node name, node value, attribute name, parent node. I tried using a recursive function to read data but its not working correctly. Please help with a simple solution to read data. (precisely a dynamic one which can handle any number of nodes and attributes)

Required Output format

   name     |   Value        | IsNode | Parent
------------------------------------------------
 updated    2014-01-01  false   record
 position   ambassador  true    record/person
 first_name Eliyahu     true    record/person/names
 alias      Eli         true    record/person/names/aliases
 country    ISRAEL      true    record/details/countries
 category   DIPLOMAT    false   record

Json Input format

{
  "Node": "record",
  "NodeValue": null,
  "ParentNode": "records",
  "Nodes": [
    {
      "Node": "person",
      "NodeValue": null,
      "ParentNode": "record",
      "Nodes": [
        {
          "Node": "title",
          "NodeValue": "Dr",
          "ParentNode": "person",
          "Nodes": [

          ],
          "Attributes": [

          ]
        },
        {
          "Node": "position",
          "NodeValue": "Ambassador",
          "ParentNode": "person",
          "Nodes": [

          ],
          "Attributes": [

          ]
        },
        {
          "Node": "names",
          "NodeValue": null,
          "ParentNode": "person",
          "Nodes": [
            {
              "Node": "first_name",
              "NodeValue": "Eliyahu",
              "ParentNode": "names",
              "Nodes": [

              ],
              "Attributes": [

              ]
            },
            {
              "Node": "last_name",
              "NodeValue": "BEN TURA",
              "ParentNode": "names",
              "Nodes": [

              ],
              "Attributes": [

              ]
            },
            {
              "Node": "aliases",
              "NodeValue": null,
              "ParentNode": "names",
              "Nodes": [
                {
                  "Node": "alias",
                  "NodeValue": "BEN TURA,Eli",
                  "ParentNode": "aliases",
                  "Nodes": [

                  ],
                  "Attributes": [

                  ]
                }
              ],
              "Attributes": [

              ]
            },
            {
              "Node": "alternative_spelling",
              "NodeValue": null,
              "ParentNode": "names",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "agedata",
          "NodeValue": null,
          "ParentNode": "person",
          "Nodes": [
            {
              "Node": "age",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            },
            {
              "Node": "as_of_date",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            },
            {
              "Node": "dob",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            },
            {
              "Node": "deceased",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        }
      ],
      "Attributes": [
        {
          "Key": "ssn",
          "Value": ""
        },
        {
          "Key": "e-i",
          "Value": "M"
        }
      ]
    },
    {
      "Node": "details",
      "NodeValue": null,
      "ParentNode": "record",
      "Nodes": [
        {
          "Node": "passports",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "passport",
              "NodeValue": null,
              "ParentNode": "passports",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "country",
                  "Value": ""
                },
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "place_of_birth",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [

          ],
          "Attributes": [
            {
              "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
              "Value": "true"
            }
          ]
        },
        {
          "Node": "locations",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "location",
              "NodeValue": null,
              "ParentNode": "locations",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "country",
                  "Value": "SENEGAL"
                },
                {
                  "Key": "city",
                  "Value": "Dakar"
                },
                {
                  "Key": "state",
                  "Value": "Dakar"
                },
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "countries",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "country",
              "NodeValue": "ISRAEL",
              "ParentNode": "countries",
              "Nodes": [

              ],
              "Attributes": [

              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "companies",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "company",
              "NodeValue": null,
              "ParentNode": "companies",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "keywords",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "keyword",
              "NodeValue": null,
              "ParentNode": "keywords",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        }
      ],
      "Attributes": [

      ]
    }
  ],
  "Attributes": [
    {
      "Key": "category",
      "Value": "DIPLOMAT"
    },
    {
      "Key": "editor",
      "Value": ""
    },
    {
      "Key": "entered",
      "Value": "2010-11-19"
    },
    {
      "Key": "sub-category",
      "Value": "PEP"
    },
    {
      "Key": "uid",
      "Value": "1389120"
    },
    {
      "Key": "updated",
      "Value": "2014-01-01"
    }
  ]
}

2 Answers 2

3

You can use Json.NET to deserialize the JSON into the following class:

public class JsonNodes
{
    public string Node { get; set; }
    public string NodeValue { get; set; }
    public string ParentNode { get; set; }
    public List<JsonNodes> Nodes { get; set; }
    public List<JsonNodesAttribute> Attributes { get; set; }
}

public class JsonNodesAttribute
{
    public string Key { get; set; }
    public string Value { get; set; }
}

and you can use this method to print all the values in the required output format using a console application:

private static void PrintValues(JsonNodes nodes, string parent)
{
    Console.WriteLine(string.Format("Name: {0}", nodes.Node));
    Console.WriteLine(string.Format("Value: {0}", nodes.NodeValue));
    Console.WriteLine("IsNode: true");
    Console.WriteLine(string.Format("Parent: {0}", parent)); 
    Console.WriteLine();

    if (parent == string.Empty)
    {
        parent += nodes.Node;
    }
    else
    {
        parent += string.Format("/{0}", nodes.Node);
    }

    foreach (JsonNodesAttribute attribute in nodes.Attributes)
    {
        Console.WriteLine(string.Format("Name: {0}", attribute.Key));
        Console.WriteLine(string.Format("Value: {0}", attribute.Value));
        Console.WriteLine("IsNode: false");
        Console.WriteLine(string.Format("Parent: {0}", parent));
    }

    Console.WriteLine();

    foreach (JsonNodes childNode in nodes.Nodes)
    {
        PrintValues(childNode, parent);
    }
}

Let's say you put the JSON to a string variable named jsonInput, here's how you deserialize and print all of its values:

string jsonInput = ...; // put the above json here
JsonNodes nodes = JsonConvert.DeserializeObject<JsonNodes>(jsonInput);
PrintValues(nodes, string.Empty);

Console.ReadLine();
Sign up to request clarification or add additional context in comments.

5 Comments

i have the object format of the json. how can i write a iterative function to read all values(precisely a dynamic one which can handle any number of nodes and attributes)
See my edited answer, the PrintValues method will run recursively until the last node.
thanks. this help to read all the nodes. But i need to obtain data in a specified format. please see the edited question
attributes should also have the full path struture
0

Use the following method to enumerate your JSON string using Json.NET:

void JsonVisitor(JToken node, int indent = 0)
{
    var indentString = new String(' ', indent * 4);
    Console.WriteLine("{0}+{1}: {2}", indentString, node["Node"], node["NodeValue"]);
    foreach (var attribute in node["Attributes"])
    {
        Console.WriteLine("{0} {1}: {2}", indentString, attribute["Key"], attribute["Value"]);
    }
    foreach (var subNode in node["Nodes"])
    {
        JsonVisitor(subNode, indent + 1);
    }
}

You can call it like this (if jsonString is your input):

var jsonObject = JObject.Parse(jsonString);
JsonVisitor(jsonObject.Root);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.