Kosmo Kosmo - 3 months ago 10
JSON Question

Parsing large json

I'm trying to parse a json file and then operate on it to insert data into a SQL Server 2008 Database.

Example:

var sr = new StreamReader("C:\\path to file\file.json");
var json = JsonSerializer.SerializeToString(sr);
var o = JsonObject.Parse(json);


But I always get this error at the second line - "Timeouts are not supported on this stream."

The Json file looks like this:

"main":{
"prg": [
{
"Id": 1,
"name": "A&E",
more fields
}
"prg": [
{
"Id": 2,
"name": "asda",
more fields
}
}


I need to make something like this

foreach (prg in main)
entity.id = prg.id
entity.name = prg.name


How can I do this and why I get that timeout exception?

EDIT: To better understand my question this is how I do for an XML file

XmlDocument sourceDoc = new XmlDocument();
sourceDoc.Load(SourcesElement2); // where SourcesElement2 is the path to my XML
XmlNodeList prg = sourceDoc.GetElementsByTagName("prg");

foreach (XmlNode item in prg)
{
entity.Name= item.SelectSingleNode("name").InnerText;
...
}


I have converted the XML to Json and I want to do same thing. For every "prg" node in the Json File insert a new item in the database

EDIT2:

This is what I've done.

using (
StreamReader stream =
File.OpenText(
"C:\\path\\Sources.json")
)
{
JObject sources = (JObject) JToken.ReadFrom(new JsonTextReader(stream));

var a = sources["on"];
var b = a["sources"];
var c = b["prgs"];
foreach (var item in c)
{
var d= item.SelectToken("prg");

// Here d is null

}


I have the same question as the one from above. For every "prg" node in the Json File insert a new item in the database. How can I do this ? ( path to prg is on/sources/prgs/ )

Answer

For everyone with the same problem here is what I've done ( NOTE: this is just an example )

By the way, thank you for everyone who tried to answer my question and I'm sorry for my mistakes.

        List<string> d = new List<string>();

        using (
            StreamReader stream =
                File.OpenText(
                    "C:\\path\\Sources.json")
            )
        {
            JObject sources = (JObject) JToken.ReadFrom(new JsonTextReader(stream));

            var a = sources["on"];
            var b = a["sources"];
            var c = b["prgs"];
            foreach (JObject item in c["prg"].ToList())
            {
                d.Add(item.Value<string>("name"));
            }

        }

        //part below is just for testing
        foreach (var VARIABLE in d)
        {
            Console.WriteLine(VARIABLE);
        }
        Console.ReadLine();