Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory for JObject

I have a problem when I try to parse a large json file, which has around 200mb. I'm doing it with Newtonsoft.Json. It gives OutOfMemory exception.

This is my code:

using (StreamReader sr=File.OpenText("path"))
        {
            JObject file= (JObject)JToken.ReadFrom(new JsonTextReader(sr));
        }

How can I do this ? ( preferable using JObject )

like image 809
Cosmin Avatar asked Nov 01 '25 08:11

Cosmin


1 Answers

You can use JsonTextReader to read text in a DataReader fashion as stated in this question:

Incremental JSON Parsing in C#

You will have to code your own logic to process JSON data, but it will for sure solve your memory issues:

using (var reader = new JsonTextReader(File.OpenText("path")))
{
    while (reader.Read())
    {
        // Your logic here (anything you need is in [reader] object), for instance:
        if (reader.TokenType == JsonToken.StartArray)
        {
            // Process array
            MyMethodToProcessArray(reader);
        }
        else if (reader.TokenType == JsonToken.StartObject)
        {
            // Process object
            MyMethodToProcessObject(reader);
        }
    }
}

You would actually build a recursive JSON parser.

like image 120
German Latorre Avatar answered Nov 02 '25 23:11

German Latorre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!