Kathiresan Jeyapandian
Kathiresan Jeyapandian

Reputation: 203

How to Convert following String to Json using Java?

I need to convert the following string into json format.

Below is the input as well as the expected output for reference.

Input:

Employee Driver Report - EDR
--------------------------------
Employee Nbr: 123480 Employee Type: DI Cat: UPL
Driver License: PP3P30 Plate: ROWP
Part Number: 1006096

Output:

{
"Employee Nbr": "123480",
"Employee Type": "DI",
"Cat": "UPL",
"Driver License": "PP3P30",
"Plate": "ROWP",
"Part Number": "1006096",
}

Sample Code:

Map<String, String> keyValueMap = new HashMap<String, String>();
String[] lines = rawText.split(System.getProperty("line.separator"));
for(String line: lines) {
.......
   keyValueMap.put(keyAndValues[i], keyAndValues[i + 1]);
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(keyValueMap);

Could you please help me on how to resolve this?

Thanks in advance.

Upvotes: 0

Views: 56

Answers (2)

npinti
npinti

Reputation: 52185

You could use an expression like so: ([\w\s]+\s*):\s*([\w]+) (example here) to process the data. This expression assumes that there is no white space within your value parameters.

Thus given the code below:

    String source = "Employee Driver Report - EDR\n" +
                    "--------------------------------\n" +
                    "Employee Nbr: 123480 Employee Type: DI Cat: UPL\n" +
                    "Driver License: PP3P30 Plate: ROWP\n" +
                    "Part Number: 1006096";

    String[] lines = source.split("\n");
    Pattern p = Pattern.compile("([\\w\\s]+\\s*):\\s*([\\w]+)");
    System.out.println("{");
    for(int i = 2; i < lines.length; i++)
    {
        Matcher m = p.matcher(lines[i]);
        while(m.find())
        {                
            System.out.println("\"" + m.group(2).trim() + "\":" + "\"" + m.group(1).trim() + "\"");
        }
    }
    System.out.println("}");

You would get something like so:

{
"Employee Nbr":"123480"
"Employee Type":"DI"
"Cat":"UPL"
"Driver License":"PP3P30"
"Plate":"ROWP"
"Part Number":"1006096"
}

EDIT: As per your comment:

    String source = "Employee Driver Report - EDR\n" +
                    "--------------------------------\n" +
                    "Employee Nbr: 123480 With white space Employee Type: DI Cat: UPL\n" +
                    "Driver License: PP3P30 Plate: ROWP\n" +
                    "Part Number: 1006096";

    String[] lines = source.split("\n");
    Pattern p = Pattern.compile("(Employee Nbr|Employee Type|Cat|Driver License|Plate|Part Number)\\s*:\\s*(.+?)(?:(?=Employee Nbr|Employee Type|Cat|Driver License|Plate|Part Number)|$)");
    System.out.println("{");
    for(int i = 2; i < lines.length; i++)
    {
        Matcher m = p.matcher(lines[i]);
        while(m.find())
        {                
            System.out.println("\"" + m.group(1).trim() + "\":" + "\"" + m.group(2).trim() + "\"");
        }
    }
    System.out.println("}");

Yields:

{
"Employee Nbr":"123480 With white space"
"Employee Type":"DI"
"Cat":"UPL"
"Driver License":"PP3P30"
"Plate":"ROWP"
"Part Number":"1006096"
}

A description of the updated expression is available here.

Upvotes: 0

Ga&#235;l J
Ga&#235;l J

Reputation: 15050

For each line you get, you have to parse it using : and (space) as delimiters.

I'll let you search the correct regex to use and ask for help if needed ;)

Upvotes: 1

Related Questions