JSON string to List

I’m grabbing a json string from a mysql database with php. What I want to do is to be able to save that json as a CSV file. I know how to make that file with a list, but I’m not sure how to do it with a json string.
I know it has to be parsed first, the I would imagine add it to the list, but I’m missing something.
public void SaveOilToCSV(string CSVList)
{
Debug.Log (CSVList);

		var oilchangelist = JSON.Parse(CSVList);
		foreach(JSONNode oc in oilchangelist)
		{
			oilchangelist.Add (oc);
  		}

It spits out an error: InvalidOperationException: Collection was modified; enumeration operation may not execute.

Just for jollys, here is the rest of the code to give you a frame of reference:

List<string>OilChangeList =new List<string>() ;
		OilChangeList.Add(CSVList.ToList());
				StringBuilder csvcontent = new StringBuilder();
				csvcontent.AppendLine("Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price");
				for(int i=0; i <OilChangeList.Count; i++ )
				{
					csvcontent.AppendLine (OilChangeList<em>.ServiceDate+", "+OilChangeList_.Location+", "+OilChangeList*.Mileage+"," +*_</em>

" “+OilChangeList.Labor+”, “+OilChangeList_.OilBrand+”, “+ OilChangeList*.OilPrice+”, "
+OilChangeList.FilterBrand+", "+OilChangeList.FilterPrice+", "+OilChangeList.OilFilterPurchaseLocation);
}*_

* string csvpath= “D:\xyz.csv”;*
* File.AppendAllText(csvpath, csvcontent.ToString());*
Thanks in advance for the help!

Sorry but your question is missing pretty much all important information. Also this makes no sense whatsoever:

foreach(JSONNode oc in oilchangelist)
{
    oilchangelist.Add (oc);
}

You iterate over the nodes in “oilchangelist” (which probably is a JSONArray) and for each element in the collection you add the same element to the same collection again. Lucky for you the internally used List does not allow modifing the list (adding / removing) when iterating through an Enumerator.

We don’t know how your JSON data looks like and what data it contains. Also we don’t know what your CSV output should look like.

Assuming your json looks something like this:

[
    {
        "ServiceDate":"aaa",
        "Location":"bbb",
        "Mileage":"ccc",
        "Labor":"ddd",
        "OilBrand":"eee",
        "OilPrice":"fff",
        "FilterBrand":"ggg",
        "FilterPrice":"hhh",
        "OilFilterPurchaseLocation":"iii"
    },
    {
        "ServiceDate":"aaa2",
        "Location":"bbb2",
        "Mileage":"ccc2",
        "Labor":"ddd2",
        "OilBrand":"eee2",
        "OilPrice":"fff2",
        "FilterBrand":"ggg2",
        "FilterPrice":"hhh2",
        "OilFilterPurchaseLocation":"iii2"
    }
]

and your output should look like this:

Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price, Filter Location
aaa,bbb,ccc,ddd,eee,fff,ggg,hhh,iii
aaa2,bbb2,ccc2,ddd2,eee2,fff2,ggg2,hhh2,iii2

You would just do:

var data = JSON.Parse(yourJSONText);
StringBuilder csv = new StringBuilder();
csv.AppendLine("Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price, Filter Location");
foreach(JSONNode n in data)
{
    csv.Append(n["ServiceDate"].Value).Append(",");
    csv.Append(n["Location"].Value).Append(",");
    csv.Append(n["Mileage"].Value).Append(",");
    csv.Append(n["Labor"].Value).Append(",");
    csv.Append(n["OilBrand"].Value).Append(",");
    csv.Append(n["OilPrice"].Value).Append(",");
    csv.Append(n["FilterBrand"].Value).Append(",");
    csv.Append(n["FilterPrice"].Value).Append(",");
    csv.Append(n["OilFilterPurchaseLocation"].Value).AppendLine();
}

string outputCSV = csv.ToString();

Be warned that “AppendAllText” will append to the end of the file and keeps the current content at the start. This makes no sense in your case since you write a new CSV header. So appending makes no sense. You probably want to use WriteAllText which will replace all content in the target file.

Note that if one of your fields actually contain problematic characters (which are the comma, new line characters as well as double quotes) the field should be enclosed in double quotes and contained quotes should be duplicated. This one might help

If that’s not what you wanted to do, you have to edit your question and add the relevant details I’ve mentioned at the beginning.