-1

I am working on a c# application that receives a lot of Json and I would like to take this Json data (currently in a string variable) and save it as a CSV file.

I have done some research but I have no idea how to tackle this task?

Edit -

from my previous question there did not seem to be a simple way to do what I wanted. From further research I have found that it is possible to create a CSV file from a dictionary.

I now have the data in this type of variable:

Dictionary<string, List<string>> dictionary; 

How can I export this ^ to a csv? Also the data in the dictionary is unknown at runtime, so I would like to separate with a ',' but its unknown is the data contains an ',' in the body. Is there a way to check and escape from this?

Community
  • 1
  • 1
brian4342
  • 1,265
  • 8
  • 33
  • 69
  • How is CSV a good format for dynamic content? Having asked that, you can combine @LarsHoldgaard answer with [Json.NET library](http://james.newtonking.com/json)(that supports dynamic serialization) and it will work (maybe reflection would also help). – keenthinker May 10 '14 at 15:57
  • Maybe it would be helpful to explain why it must be exported to csv? CSV is typically used to present tabular data, but if every row is different then it basically eliminates the benefit of csv. Maybe there is an alternative that will get the result you need that you haven't thought of and csv is not really the answer? – Tim Hobbs May 10 '14 at 17:45

2 Answers2

1

I would convert this into C# objects using JSON.NET . Then afterwards, I would iterate my C# objects and write the values in the order you want.

That should be simple :)

Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
  • Ok but let assume that I dont know the structure of the json that I am being given and each json string is completely different. So is there a way to convert this json string into a csv file or maybe even a different type that can map easier to CSV without having to create a class for each potential json string i get? – brian4342 May 10 '14 at 15:39
  • Would it be easier to convert XML to a csv file? @lars – brian4342 May 10 '14 at 15:40
  • Ah, that makes it more difficult :) What you could do, even though it probably wouldn't make a lot of sense, is to use reflection. You could iterate all tokens+properties in the JObject, and write to CSV? See some code here: http://stackoverflow.com/questions/13652983/dynamic-jcontainer-json-net-iterate-over-properties-at-runtime – Lars Holdgaard May 10 '14 at 15:46
  • No matter what, you are going to loose some of the depth of the JSON file. So it also depends on what you are going to use it for ... – Lars Holdgaard May 10 '14 at 15:47
  • Well my application is being sent json or can switch to xml. And I need to export this to a CSV file? – brian4342 May 10 '14 at 15:53
  • Keep in mind that either of these (JSON or XML) can send an object graph and that just flattening it into a CSV is not always going to make sense so there isn't a general purpose tool to convert arbitrary JSON to CSV. Because of that you are really better off converting to C# objects first and then exporting them to CSV even if it means that you need to create DTOs for each possible JSON object. – Yaur May 10 '14 at 15:58
0

I guess first lets take a small look to CSV files..

What is csv file ? Actually its self-explained (Comma Seperated Values) but simply A CSV file is a file which carries/stores the different values with seperation of a Comma (something like ',' or ';' or other else )

So when you want to create a csv file you can simply iterate your values and seperate it with some chars which you decide..

Just be careful that your delimiters (the characters which you decide to use to seperate different values from others) shouldn't be in your values..

Say you have this poem to store and you want to seperate from poem-rows with the delimiter "," :

When evening comes
Mails singing misses,
But Zagreb(Croatian) Radio the Lili Marleen Song
....
We, All Human-kind sweared : 
we believed on Freedom,
We, sweared for the sake of Freedom!
.....
[ Attila ILHAN ]

what will happen? you got unexpected results cause the poem has your delimiter in its body..

with a pseudo :

1-) decide your delimiter(s) as data bodies don't have 2-) get your data as string 3-) Append with your delimiter to your csv file

EDIT - After op comments to me, changes his question-body with useful knowledges from other commentators' comments..

PSEUDO :

string delimiter = "##";

string Path = yourCSVFilePathToSave;

bool append = true;

string HexadecimalFormat = "X";

Encoding enc = Encoding.UTF-8;

int bufferSize = 4096;

int[] intArray = {1,2,3,4,5,6,7,8,9};

  using ( var writer = new StreamWriter(Path, append, enc, bufferSize))
  {
     foreach ( int i in intArray)
     {
       writer.Write (HexadecimalFormat, i);
       writer.Write (HexadecimalFormat, delimiter);
     }
   }

PSEUDO To Read :

List<string> list = new List<string>();
       using (var reader = new StreamReader(Path, enc))
       { 
           list.AddRange(reader.ReadLine().Split(new char[] { '#', '#' }, StringSplitOptions.RemoveEmptyEntries).ToList());
       }
sihirbazzz
  • 708
  • 4
  • 20
  • The body of the data I receive is unknown until I get it, so assuming I want to separate with a ',' It will need some form of validation. Also I know what I CSV file is, so how can I use your question to solve my problem? – brian4342 May 10 '14 at 16:09