2

i have a large json data and i want to get all paths from the root until getting value for the paths then storing the result to a string as i described bellow here is my json for example

{  
   "root":{  
      "first":{  
         "first1":{  
            "value":"1"
         },
         "first2":{  
            "value":"2"
         },
         "first3":{  
            "value":"3"
         }
      },
      "second":{  
         "second1":{  
            "value":"1"
         },
         "second2":{  
            "value":"2"
         },
         "second3":{  
            "value":"3"
         }
      },
      "third":{  
         "third1":{  
            "value":"1"
         },
         "third2":{  
            "value":"2"
         },
         "third3":{  
            "value":"3"
         }
      },
      "four":{  
         "value":"4"
      },
      "five":{  
         "five1":{  
            "five11":{  
               "value":"five11"
            },
            "five12":{  
               "value":"five12"
            }
         },
         "five2":{  
            "five21":{  
               "five211":{  
                  "value":"five211"
               }
            }
         }
      }
   }
}

then i want to make each paths like bellow dynamically in c# and showing in screen please tell me a way to make this

root.first.first1.value
root.first.first2.value
root.first.first3.value

root.second.second1.value
......

root.four.value

root.five.five1.five11.value
root.five.five1.five12.value
....
root.five2.five21.five211.value 
4
  • 1
    People have suggesting things to you and you and you then ask but does it do X or what about Y. Where is your code - what have you tried, What happens when you try the things suggested to you - have you tried anything using the ideas presented to you and then debugged to see what you can do. What have you done ??? Commented Dec 7, 2014 at 9:05
  • thank you for your advice but i tried their ideas before , if you see this one: string value= (string)obj["four"]["value"]; this one can you apply to a json data that contains 20000 different nodes and paths ? if you can do it please help me. Commented Dec 7, 2014 at 9:30
  • Then you really should show ant current example so that people can see where you've got to and what you've tried, so that you don't waste there time and they can give you more helpful answers. @Ibn gave you the only real answer that's going to work for a large (variable) number of lines with nodes in a tree that are only going to be known at run time. And that recursion. Commented Dec 8, 2014 at 10:07
  • Yes , i think my question was so clear and Ibn understood my question , then he send me exact answer , thank you to you and lbn Commented Dec 8, 2014 at 10:34

1 Answer 1

6

Use JSON.NET and iterate recursively through the Children property and check if the current token doesn't have HasValues set to true and if that's the case add the Path property of that token to a StringBuilder or what have you. Should give you exactly what you want.

Edith: Code Sample

I was lazy and just included the whole console application code.

Example on dotnetfiddle.net

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

public class Program
{
    public static void Main()
    {
            var json = @"
{  
   ""root"":{  
      ""first"":{  
         ""first1"":{  
            ""value"":""1""
         },
         ""first2"":{  
            ""value"":""2""
         },
         ""first3"":{  
            ""value"":""3""
         }
      }
    }
}";

        var jobject = JObject.Parse (json);
        var sb = new StringBuilder ();

        RecursiveParse (sb, jobject);

        Console.WriteLine (sb.ToString());
    }

    public static void RecursiveParse(StringBuilder sb, JToken token)
    {
        foreach (var item in token.Children()) {
            if (item.HasValues)
            {
                RecursiveParse (sb, item);
            } else {
                sb.AppendLine (item.Path);
            }
        }

    }   
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you, i think your idea may work , but i need the code , if you have time to send me the code i appreciate it.
Will add an example when I'm somewhere near a computer.
Please can you send me the example ?
What's wrong with the code I provided in the answer? Isn't that enough?

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.