Results 1 to 3 of 3
  1. #1
    phaniakella is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Convert CSV to JSON. JSON payload needs to be in between square brackets (as root)

    Hi Experts

    I have a requirement to convert CSV to JSON. Assume if CSV files have got 2 rows. I need to create a JSON file for each row. This means output will be 2 json files.

    Sample Test data: Test.csv

    ID,fileName,Type,Direction,Date
    1001,File1,PDF,Out,20180518
    1002,File2,DOC,Out,20180502


    I have written below code for achieving this. The code is working fine and creating individual JSON files for each row as:

    File1.txt

    {
    "ID" : "1001",
    "fileName" : "File1",
    "Type" : "PDF",
    "Direction" : "Out",
    "Date" : "20180518"
    }

    File2.txt

    {
    "ID" : "1002",
    "fileName" : "File2",
    "Type" : "DOC",
    "Direction" : "Out",
    "Date" : "20180502"
    }

    I need to include the JSON payload in square brackets as root. E.g. Output should be as below. How can I achieve this

    File1.txt

    [

    {
    "ID" : "1002",
    "fileName" : "File2",
    "Type" : "DOC",
    "Direction" : "Out",
    "Date" : "20180502"
    }

    ]

    File2.txt

    [

    {
    "ID" : "1002",
    "fileName" : "File2",
    "Type" : "DOC",
    "Direction" : "Out",
    "Date" : "20180502"
    }

    ]

    Code:

    Java Code:
    package com.sap.csv2json;
    
    import java.io.File;
    
    import java.util.List;
    
    import java.util.Map;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import com.fasterxml.jackson.databind.SerializationFeature;
    
    import com.fasterxml.jackson.dataformat.csv.CsvMapper;
    
    import com.fasterxml.jackson.dataformat.csv.CsvSchema;
    
    public class ConvertCSVtoJson {
    
    @SuppressWarnings("unchecked")
    
    public static void main(String[] args) throws Exception {
    
            File input = new File("C:\\Users\\akellap\\RND\\JSON\\Source\\Test.csv");
    
            CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
    
            CsvMapper csvMapper = new CsvMapper();
    
            // Read data from CSV file
    
            List<Object> readAll = csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll();
    
                        ObjectMapper mapper = new ObjectMapper();
    
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
    
            // Write JSON formated data to output.json file
    
               for (Object row : readAll) {
    
            Map<String, String> map = (Map<String, String>) row;
    
            String fileName = map.get("fileName");
    
            File output = new File("C:\\Users\\akellap\\RND\\JSON\\Target\\"+fileName+".txt");
    
             mapper.writerWithDefaultPrettyPrinter().writeValue(output, row);
    
                    }
    
              }
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Convert CSV to JSON. JSON payload needs to be in between square brackets (as root

    Can't you just write out the appropriate square bracket at the beginning and end of the file? I don't see a problem here.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,960
    Rep Power
    31

    Default Re: Convert CSV to JSON. JSON payload needs to be in between square brackets (as root

    What args does the writeValue method take? Is there a way to intercept what it writes to allow wrapping its output in the []s?
    Are there overloaded versions of the method that take different args?
    Does the method append to an existing file or write over the contents of an existing file?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Convert Json Format
    By jeffwang66 in forum New To Java
    Replies: 1
    Last Post: 10-29-2014, 01:25 AM
  2. convert xml to JSON using XPath dynamically
    By blertadhimitri in forum New To Java
    Replies: 2
    Last Post: 11-17-2013, 01:40 PM
  3. How to convert xml to json using JSONWriter?
    By blertadhimitri in forum New To Java
    Replies: 3
    Last Post: 11-08-2013, 02:13 PM
  4. could not convert the xml to json using java
    By cgk_js in forum Advanced Java
    Replies: 0
    Last Post: 11-30-2012, 09:31 AM
  5. convert html to json
    By nigina in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •