skip to Main Content

input:
{
"First Name": "XX",
"Middle Name": "",
"Last Name": "XX",
"Date of Birth": "11/12/2013",
"Address": "XXX",
"Village": "XXX",
"Pincode": "34234"
"License Number": "1234567890"
}

output:

{
"First Name": "XX",
"Middle Name": "",
"Last Name": "XX",
"Date of Birth": "11/12/2013",
"Address":
{
"Village": "XXX",
"Pincode": "34234"
}
"License Number": "1234567890"
}

Hi ,
I have the above input json and I plan to generate this kind of output json. Similar kind of use cases.
Now I want to generate one json schema for each use case using sample output json.
With the help of Java, I would like to dynamically pick the values and prepare the output format as expected.

Anyone achieved similar kind of job ?

input:
{
"First Name": "XX",
"Middle Name": "",
"Last Name": "XX",
"Date of Birth": "11/12/2013",
"Address": "XXX",
"Village": "XXX",
"Pincode": "34234"
"License Number": "1234567890"
}

output:

{
"First Name": "XX",
"Middle Name": "",
"Last Name": "XX",
"Date of Birth": "11/12/2013",
"Address":
{
"Village": "XXX",
"Pincode": "34234"
}
"License Number": "1234567890"
}

Hi ,
I have the above input json and I plan to generate this kind of output json. Similar kind of use cases.
Now I want to generate one json schema for each use case using sample output json.
With the help of Java, I would like to dynamically pick the values and prepare the output format as expected.

Anyone achieved similar kind of job ??

2

Answers


  1. To get the output that you want you muste create a custom class which has the attributes, like that:

    Class Address

    class Address{
    
        @JsonProperty("Address")
        private String address;
    
        @JsonProperty("Village")
        private String village;
    
        @JsonProperty("Pincode")
        private String pinCode;
    }
    

    Then the Class which contains all CustomData, like that:

    class CustomData {
        @JsonProperty("First Name")
        private String firstName;
    
        @JsonProperty("Middle Name")
        private String middleName;
    
        @JsonProperty("Last Name")
        private String lastName;
    
        @JsonProperty("Date of Birth")
        private String dateOfBirth;
    
        private Address address = new Address();
    
        @JsonProperty("License Number")
        private String licenseNumber;
    
        // Getters and setters
    
        @JsonProperty("Address")
        private void setAddress(String val){
            address.setAddress(val);
        }
    
        @JsonProperty("Village")
        private void setVillage(String val){
            address.setVillage(val);
        }
    
        @JsonProperty("Pincode")
        private void setPinCode(String val){
            address.setPinCode(val);
        }
    }
    

    TEST:

     public static void main(String[] args) throws Exception {
            // Input JSON
            String inputJson = """
                    {
                      "First Name": "XX",
                      "Middle Name": "",
                      "Last Name": "XX",
                      "Date of Birth": "11/12/2013",
                      "Address": "XXX",
                      "Village": "XXX",
                      "Pincode": "34234",
                      "License Number": "1234567890"
                    }
                    """;
    
            CustomData res = JsonMapper.builder().build().readValue(inputJson, CustomData.class);
    
            System.out.println(JsonMapper.builder().build().writerWithDefaultPrettyPrinter().writeValueAsString(res));
    
        }
    }
    

    OUTPUT:

    {
      "First Name" : "XX",
      "Middle Name" : "",
      "Last Name" : "XX",
      "Date of Birth" : "11/12/2013",
      "Address" : {
        "Address" : "XXX",
        "Village" : "XXX",
        "Pincode" : "34234"
      },
      "License Number" : "1234567890"
    }
    

    Try it and let me know.

    Login or Signup to reply.
  2. You may try to use JSON libraries with query, transform and restructure capability such as Josson.

    https://github.com/octomix/josson

    Josson josson = Josson.fromJsonString(
        "{" +
        "    "First Name": "XX"," +
        "    "Middle Name": ""," +
        "    "Last Name": "XX"," +
        "    "Date of Birth": "11/12/2013"," +
        "    "Address": "XXX"," +
        "    "Village": "XXX"," +
        "    "Pincode": "34234"," +
        "    "License Number": "1234567890"" +
        "}");
    JsonNode node = josson.getNode("field(Address: map(Village, Pincode), Village:, Pincode:)");
    System.out.println(node.toPrettyString());
    

    Output

    {
      "First Name" : "XX",
      "Middle Name" : "",
      "Last Name" : "XX",
      "Date of Birth" : "11/12/2013",
      "Address" : {
        "Village" : "XXX",
        "Pincode" : "34234"
      },
      "License Number" : "1234567890"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search