Combine Multiple JSON Files Json.NET -


i have api receives json calls push files (800kb-1mb) (1 each call), , have hourly task takes of json files in last hour , combines them single file make better daily/monthly analytics on.

each file consists of collection of data, in format of [ object {property: value, ... ]. due this, cannot simple concatenation it'll no longer valid json (nor add comma file collection of collections). keep memory foot-print low possible, looking @ following example , pushing each file stream (deserializing file using jsonconvert.deserializeobject(filecontent); however, doing this, end collection of collection well. have tried using jarray instead of jsonconvert, pushing list outside of foreach provides same result. if move serialize call outside foreach, work; however, worried holding 4-6gb worth of items in memory.

in summary, i'm ending [ [ object {property: value, ... ],... [ object {property: value, ... ]] desired output [ object {property: value (file1), ... object {property: value (filen) ].

        using (filestream fs = file.open(@"c:\users\public\documents\combined.json", filemode.createnew))         {             using (streamwriter sw = new streamwriter(fs))             {                 using (jsonwriter jw = new jsontextwriter(sw))                 {                     jw.formatting = formatting.none;                      jarray list = new jarray();                     jsonserializer serializer = new jsonserializer();                      foreach (ilistblobitem blob in blobcontainer.listblobs(prefix: "sharepointblobs/"))                     {                         if (blob.gettype() == typeof(cloudblockblob))                         {                             var blockblob = (cloudblockblob)blob;                             var content = blockblob.downloadtext();                             var deserialized = jarray.parse(content);                             //deserialized = jsonconvert.deserializeobject(content);                             list.merge(deserialized);                             serializer.serialize(jw, list);                         }                         else                         {                             console.writeline("non-block-blob: " + blob.storageuri);                         }                     }                 }             }         } 
share|improve question
up vote 1 down vote accepted

in situation, keep processing , memory footprints low, think concatenate files 1 after other though results in technically invalid json. deserialize combined file later, can take advantage of supportmultiplecontent setting on jsontextreader class , process object collections through stream if 1 whole collection. see this answer example of how this.

share|improve answer
    
that looks need - surprised issue common enough made setting! thank you. – steven mayer feb 9 '16 @ 6:24
    
i surprised when first came across it, has come in handy more once. – brian rogers feb 9 '16 @ 6:27

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments