Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one csv file and I need to convert that data into json format in json. I tried to read file and get data and split by comma (,) seperator. And then convert that data into json format. But the result is not expected result. Because come column values have multiple (,) seperator.

Any one can help me to solve this issue ?


Coming Result was : [["JOB ID","JOB NAME","ROLE DESCRIPTION/EXPECTATION","RESPONSIBILITIES","PREFERRED SKILLS PROFILE","JOB LOCATION","JOB START DATE","JOB STATUS","JOB OPEN DATE","ENGAGEMENT TYPE","FIXED PRICE (Y/N)","MILESTONE (Y/N)","HOURLY PAY (Y/N)","BUDGET CURRENCY (CURRENCY CODE)","BUDGET AMOUNT (NUMBER ONLY)","BILL CURRENCY (CURRENCY CODE)","BILL AMOUNT (NUMBERS ONLY)","TARGET SALARY CURRENCY (CURRENCY CODE)","TARGET SALARY AMOUNT (NUMBERS ONLY)","EDUCATION NAME","CERTIFICATION/LICENSURE","HIRING MANAGER","TALENT ADVISER","JOB CLUSTER KEYWORDS","CONTRACT TIME"],["","Operations Support Administrator","\"The Human Resources Operations Support Administrator is a critical member of the Human Resources team that serves approximately 62 schools and 5 administrative organizational units. The Operations Support Administrator will provide specialized"," technical support in one or more operational areas. The Operations Support Administrator will likely provide payroll and procurement support"," but will also provide support in one or more of the following operational areas: budgeting"," grants management"," contracts"," human resources"," technology"," school food"," transportation"," and/or health. The Operations Support Administrator�s specialty may vary from team to team and will depend on their prior experience.\"","\"Support a network of schools to continuously improve operations"," school support"," and customer service. Provide technical support and assistance related to assigned operational area(s). Manage systems associated with assigned operational area(s) and provide technical support to school-based staff as necessary. Collect and interpret test scores and other school performance data to help schools drive student achievement. Facilitate the preparation of statistical and narrative reports and/or visual presentations such as charts or graphs"," as appropriate. "],["Provide transactional oversight and support related to assigned operational area(s). Advocate/liaise with the district team"," the Human Resources team and central entities when necessary to ensure operational transactions are executed to meet compliance mandates and schools� satisfaction."],["Identify areas in which schools are in need of administrative and/or operational training and ensure that training opportunities are provided to each school."],["Provide guidance and resources to school-based personnel to increase the operational/administrative capacity and autonomy of each school. This includes frequent school visits to trouble-shoot and provide one-on-one support as necessary."],["Support Human Resources� service-oriented culture"," which is designed to attain high levels of principal satisfaction with the services and support the district provides."],["Provide critical"," technical operational information as available from Human Resources"," including central policy and process updates and changes."],["\"","\"Communcation Skills"," Attention to Detail"," DOE Systems"," Planning skills\"","\"New York"," New York"," USA\"","11-01-2017","OPEN","10-01-2017","CONTRACT","","","","USD","45000","USD","45000","USD","","Bachelor Degree","NYC Civil Service Status","John Jones","Carolina Smith","","6 months"]]

Expected Result is : [ { "JOB ID": "", "JOB NAME": "Operations Support Administrator", "ROLE DESCRIPTION/EXPECTATION": "The Human Resources Operations Support Administrator is a critical member of the Human Resources team that serves approximately 62 schools and 5 administrative organizational units. The Operations Support Administrator will provide specialized, technical support in one or more operational areas. The Operations Support Administrator will likely provide payroll and procurement support, but will also provide support in one or more of the following operational areas: budgeting, grants management, contracts, human resources, technology, school food, transportation, and/or health. The Operations Support Administrator�s specialty may vary from team to team and will depend on their prior experience.", "RESPONSIBILITIES": "Support a network of schools to continuously improve operations, school support, and customer service. Provide technical support and assistance related to assigned operational area(s). Manage systems associated with assigned operational area(s) and provide technical support to school-based staff as necessary. Collect and interpret test scores and other school performance data to help schools drive student achievement. Facilitate the preparation of statistical and narrative reports and/or visual presentations such as charts or graphs, as appropriate. \nProvide transactional oversight and support related to assigned operational area(s). Advocate/liaise with the district team, the Human Resources team and central entities when necessary to ensure operational transactions are executed to meet compliance mandates and schools� satisfaction.\nIdentify areas in which schools are in need of administrative and/or operational training and ensure that training opportunities are provided to each school.\nProvide guidance and resources to school-based personnel to increase the operational/administrative capacity and autonomy of each school. This includes frequent school visits to trouble-shoot and provide one-on-one support as necessary.\nSupport Human Resources� service-oriented culture, which is designed to attain high levels of principal satisfaction with the services and support the district provides.\nProvide critical, technical operational information as available from Human Resources, including central policy and process updates and changes.", "PREFERRED SKILLS PROFILE": "Communcation Skills, Attention to Detail, DOE Systems, Planning skills", "JOB LOCATION": "New York, New York, USA", "JOB START DATE": "11-01-2017", "JOB STATUS": "OPEN", "JOB OPEN DATE": "10-01-2017", "ENGAGEMENT TYPE": "CONTRACT", "FIXED PRICE (Y/N)": "", "MILESTONE (Y/N)": "", "HOURLY PAY (Y/N)": "", "BUDGET CURRENCY (CURRENCY CODE)": "USD", "BUDGET AMOUNT (NUMBER ONLY)": 45000, "BILL CURRENCY (CURRENCY CODE)": "USD", "BILL AMOUNT (NUMBERS ONLY)": 45000, "TARGET SALARY CURRENCY (CURRENCY CODE)": "USD", "TARGET SALARY AMOUNT (NUMBERS ONLY)": "", "EDUCATION NAME": "Bachelor Degree", "CERTIFICATION/LICENSURE": "NYC Civil Service Status", "HIRING MANAGER": "John Jones", "TALENT ADVISER": "Carolina Smith", "JOB CLUSTER KEYWORDS": "", "CONTRACT TIME": "6 months" } ]

c#

What I have tried:

My code is below:

`string targetFile = @"C:\Users\grajaji\Desktop\target.csv";
var csv = new List(); var lines = System.IO.File.ReadAllLines(targetFile);enter code here

foreach (string line in lines) csv.Add(line.Split(','));

var json = JsonConvert.SerializeObject(csv);

return Request.CreateResponse(HttpStatusCode.OK, json);`
Posted
Updated 6-Feb-19 0:49am
Comments
Richard MacCutchan 9-Nov-17 7:22am    
Please edit your question and remove all that unformatted text dump. Just show enough information (properly formatted within <pre> tags) and explain what the problem is in converting.
Mehdi Gholam 9-Nov-17 7:31am    
Your data is in JSON format not csv, so just read it with a json reader.

1 solution

C#
public void Convert2Json() 
{ 
    try 
    { 
        if (FileUpload1.PostedFile.FileName != string.Empty) 
        { 
            string[] FileExt = FileUpload1.FileName.Split('.'); 
            string FileEx = FileExt[FileExt.Length - 1]; 
            if (FileEx.ToLower() == "csv") 
            { 
                string SourcePath = Server.MapPath("Resources//"
                                                   + FileUpload1.FileName); 
                FileUpload1.SaveAs(SourcePath); 
                string Destpath = (Server.MapPath("Resources//" + FileExt[0]
                                                  + ".json")); 

                StreamWriter sw = new StreamWriter(Destpath); 
                var csv = new List<string[]>(); 
                var lines = System.IO.File.ReadAllLines(SourcePath); 
                foreach (string line in lines) 
                    csv.Add(line.Split(',')); 
                string json = new System.Web.Script.Serialization
                                       .JavaScriptSerializer().Serialize(csv); 
                sw.Write(json); 
                sw.Close(); 
                TextBox1.Text = Destpath; 
                MessageBox.Show("File is converted to json."); 
            } 
            else 
            { 
                MessageBox.Show("Invalid File"); 
            } 

        } 
        else 
        { 
            MessageBox.Show("File Not Found."); 
        } 
    } 
    catch (Exception ex) 
    { 
        MessageBox.Show(ex.Message); 
    } 
}
 
Share this answer
 
v2
Comments
Graeme_Grant 9-Nov-17 8:10am    
Formatting your answers makes a big difference...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900