c# - Creating a csv file from json with different header values per record -


i have massive json file nested. need write multiple csv files depending on name of field, if exists add values headers i've created if not create new one. working fine. have ran problem headers not match because particular header doesn't exist record. example:

header:  dog   cat  mouse  horse record1:  yes   yes  yes   yes 

// above example of file values adding record 2 header value not listed @ all

header:  dog   cat  mouse  horse record1:  yes   yes  yes   yes record2:  yes   yes  yes   *** 

record2 above not have mouse on record because doesn't line yes shifted left. need write null under header before spitting out values file. below code if great i'm lost @ point:

        static list<string> headlist = new list<string>();         static list<string> newheaderlist = new list<string>();         static list<string> valuelist = new list<string>();         static list<string> oldheadlist = new list<string>();         static void main()         {             var data = jsonconvert.deserializeobject<dynamic>(file.readalltext(                           @"c:\users\nphillips\workspace\2016r23\uitestautomation\seeddatagenerator\src\staticresources\seeddata.resource"));             string filename = "";             var bundles = data.recordsetbundles;              foreach (var bundle in bundles)             {                 var records = bundle.records;                 foreach (var record in records)                 {                     var test = record.attributes;                     foreach (var testagain in test)                     {                         // getting object name ex. location, item, etc.                         var jprop = testagain jproperty;                         if (jprop != null)                         {                             filename = jprop.first.tostring().split('_')[2] + ".csv";                         }                         break;                     }                     string header = "";                     string value = "";                     foreach (var child in record)                     {                         var thechild = child jproperty;                         if (thechild != null && !thechild.name.equals("attributes"))                         {                             // adding name , values list                             headlist.add(child.name);                             valuelist.add(child.value.tostring());                         }                     }                     // calling method write columns , values                     writecsv(headlist, valuelist, filename);                     valuelist.clear();                     headlist.clear();                 }             }         }          public static void writecsv(list<string> headlist, list<string> vallist, string filename)         {             string headerstring = "";             string value = "";              if (!file.exists(filename))             {                 foreach (var header in headlist)                 {                     foreach (var val in vallist)                     {                         value += val + ",";                     }                     oldheadlist.add(header);                     headerstring += header + ',';                 }                  headerstring += "+" + environment.newline;                 file.writealltext(filename, headerstring);             }             else             {                 foreach (var header in headlist)                 {                     foreach (var oldheader in oldheadlist)                     {                         foreach (var val in vallist)                         {                             if (header != oldheader)                             {                                 value += "null,";                             }                             else                             {                                 value += val + ",";                             }                         }                      }                 }             }             file.appendalltext(filename, value);             value += environment.newline;         }     } 

my horrific json file cannot change used company: https://codeshare.io/rgl6k5

is there kind of pattern? reason asking is, maybe create service serialize json complex object(s). once done have service serializes object csv. service know write multiple csv files needed.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -