Search code examples
javajsonstringpojo

Parse non Json String into POJO


I have to extract details from a string of raw logs and convert them into POJO. Since they are logs, they are not actually in JSON format.

eg.

{
    CONTAINER_ID = some_value,
    TRACKING_ID = value,
    PHYSICAL_ATTRIBUTES = PhysicalAttributes(length = Dimension(value = 30.0, unit = CM, type = null), width = Dimension(value = 30.0, unit = CM, type = null), height = Dimension(value = 30.0, unit = CM, type = null), scaleWeight = Weight(value = 5.0, unit = kg, type = null)),
    SHIP_METHOD = some_value,
    ADDRESS_ID = some_value,
    CUSTOMER_ID = some_value,
    REQUEST_STATE = UNKNOWN,
    REQUEST_STATE_REASON = UNKNOWN,
    RESPONSE = GetAccessPointsForHubDeliveryOutput(destinationLocation = null, fallBackLocation = null, capability = null),
    IS_COMMERCIAL_ATTRIBUTE_PRESENT = false
}

Is there anyway to convert this string into POJO? (Required objects are the capital ones)


Solution

  • Is there anyway to convert this string into POJO?

    Yes, you can write a parser that will read the data and store it into the appropriate object. One way to do this would be to convert the data to JSON format and take advantage of existing JSON parsing to convert the resulting JSON into a POJO.

    Here is a sample POJO class based on your data. Note I did a simple conversion of your data to JSON and ran that through an online code generation tool so this class does not follow Java Code Conventions.

    public class PojoClass {
        private String CONTAINER_ID;
    
        private String TRACKING_ID;
    
        private String PHYSICAL_ATTRIBUTES;
    
        private String SHIP_METHOD;
    
        private String ADDRESS_ID;
    
        private String CUSTOMER_ID;
    
        private String REQUEST_STATE;
    
        private String REQUEST_STATE_REASON;
    
        private String RESPONSE;
    
        private String IS_COMMERCIAL_ATTRIBUTE_PRESENT;
    
        public void setCONTAINER_ID(String CONTAINER_ID) {
            this.CONTAINER_ID = CONTAINER_ID;
        }
    
        public String getCONTAINER_ID() {
            return this.CONTAINER_ID;
        }
    
        public void setTRACKING_ID(String TRACKING_ID) {
            this.TRACKING_ID = TRACKING_ID;
        }
    
        public String getTRACKING_ID() {
            return this.TRACKING_ID;
        }
    
        public void setPHYSICAL_ATTRIBUTES(String PHYSICAL_ATTRIBUTES) {
            this.PHYSICAL_ATTRIBUTES = PHYSICAL_ATTRIBUTES;
        }
    
        public String getPHYSICAL_ATTRIBUTES() {
            return this.PHYSICAL_ATTRIBUTES;
        }
    
        public void setSHIP_METHOD(String SHIP_METHOD) {
            this.SHIP_METHOD = SHIP_METHOD;
        }
    
        public String getSHIP_METHOD() {
            return this.SHIP_METHOD;
        }
    
        public void setADDRESS_ID(String ADDRESS_ID) {
            this.ADDRESS_ID = ADDRESS_ID;
        }
    
        public String getADDRESS_ID() {
            return this.ADDRESS_ID;
        }
    
        public void setCUSTOMER_ID(String CUSTOMER_ID) {
            this.CUSTOMER_ID = CUSTOMER_ID;
        }
    
        public String getCUSTOMER_ID() {
            return this.CUSTOMER_ID;
        }
    
        public void setREQUEST_STATE(String REQUEST_STATE) {
            this.REQUEST_STATE = REQUEST_STATE;
        }
    
        public String getREQUEST_STATE() {
            return this.REQUEST_STATE;
        }
    
        public void setREQUEST_STATE_REASON(String REQUEST_STATE_REASON) {
            this.REQUEST_STATE_REASON = REQUEST_STATE_REASON;
        }
    
        public String getREQUEST_STATE_REASON() {
            return this.REQUEST_STATE_REASON;
        }
    
        public void setRESPONSE(String RESPONSE) {
            this.RESPONSE = RESPONSE;
        }
    
        public String getRESPONSE() {
            return this.RESPONSE;
        }
    
        public void setIS_COMMERCIAL_ATTRIBUTE_PRESENT(String IS_COMMERCIAL_ATTRIBUTE_PRESENT) {
            this.IS_COMMERCIAL_ATTRIBUTE_PRESENT = IS_COMMERCIAL_ATTRIBUTE_PRESENT;
        }
    
        public String getIS_COMMERCIAL_ATTRIBUTE_PRESENT() {
            return this.IS_COMMERCIAL_ATTRIBUTE_PRESENT;
        }
    
        @Override
        public String toString() {
            return "PojoClass [CONTAINER_ID=" + CONTAINER_ID + ", TRACKING_ID=" + TRACKING_ID + ", PHYSICAL_ATTRIBUTES="
                    + PHYSICAL_ATTRIBUTES + ", SHIP_METHOD=" + SHIP_METHOD + ", ADDRESS_ID=" + ADDRESS_ID + ", CUSTOMER_ID="
                    + CUSTOMER_ID + ", REQUEST_STATE=" + REQUEST_STATE + ", REQUEST_STATE_REASON=" + REQUEST_STATE_REASON
                    + ", RESPONSE=" + RESPONSE + ", IS_COMMERCIAL_ATTRIBUTE_PRESENT=" + IS_COMMERCIAL_ATTRIBUTE_PRESENT
                    + "]";
        }
    }
    

    I stored your sample data in a simple text file and then wrote the following code to read and convert it to JSON and then to a POJO using Gson:

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.List;
    
    import com.google.gson.Gson;
    
    public class GsonMain {
    
        public static void main(String[] args) throws IOException {
            Gson gson = new Gson();
            List<String> lines = Files.readAllLines(Paths.get("src/test/resources/input.txt"));
    
            StringBuilder builder = new StringBuilder();
            for(String line: lines){
                line = line.trim();
                if("{".equals(line) || 
                    "}".equals(line)){
                    builder.append(line);
                    continue;
                }
    
                line = line.replaceFirst(" = ", "\" : \"");
                line = line.replaceAll("^", "\"");
                line = line.replaceAll(",$", "\",");
    
                if(!line.endsWith(",")){
                    line = line + "\"";
                }
    
                builder.append(line);
            }
            System.out.println("JSON --> " + builder.toString());
    
            PojoClass pojo = gson.fromJson(builder.toString(), PojoClass.class);
            System.out.println("POJO --> " + pojo);
        }
    
    }
    

    Here is the sample output:

    JSON --> {"CONTAINER_ID" : "some_value","TRACKING_ID" : "value","PHYSICAL_ATTRIBUTES" : "PhysicalAttributes(length = Dimension(value = 30.0, unit = CM, type = null), width = Dimension(value = 30.0, unit = CM, type = null), height = Dimension(value = 30.0, unit = CM, type = null), scaleWeight = Weight(value = 5.0, unit = kg, type = null))","SHIP_METHOD" : "some_value","ADDRESS_ID" : "some_value","CUSTOMER_ID" : "some_value","REQUEST_STATE" : "UNKNOWN","REQUEST_STATE_REASON" : "UNKNOWN","RESPONSE" : "GetAccessPointsForHubDeliveryOutput(destinationLocation = null, fallBackLocation = null, capability = null)","IS_COMMERCIAL_ATTRIBUTE_PRESENT" : "false"}
    POJO --> PojoClass [CONTAINER_ID=some_value, TRACKING_ID=value, PHYSICAL_ATTRIBUTES=PhysicalAttributes(length = Dimension(value = 30.0, unit = CM, type = null), width = Dimension(value = 30.0, unit = CM, type = null), height = Dimension(value = 30.0, unit = CM, type = null), scaleWeight = Weight(value = 5.0, unit = kg, type = null)), SHIP_METHOD=some_value, ADDRESS_ID=some_value, CUSTOMER_ID=some_value, REQUEST_STATE=UNKNOWN, REQUEST_STATE_REASON=UNKNOWN, RESPONSE=GetAccessPointsForHubDeliveryOutput(destinationLocation = null, fallBackLocation = null, capability = null), IS_COMMERCIAL_ATTRIBUTE_PRESENT=false]