Search code examples
javaserializationjacksonjson-serialization

Mapping an Object with variable name in Jackson


I have the following JSON:

{
 "filebeat": {
   "version": 2,
   "modified_date": "2021-01-15T14:02:41.103Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "30gb",
             "max_age": "1d"
           }
         }
       },
       "delete": {
         "min_age": "90d",
         "actions": {
           "wait_for_snapshot": {
             "policy": "mainbackuppolicy"
           }
         }
       }
     }
   }
 },
 "ilm-history-ilm-policy": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.717Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb",
             "max_age": "30d"
           }
         }
       },
       "delete": {
         "min_age": "90d",
         "actions": {
           "delete": {
             "delete_searchable_snapshot": true
           }
         }
       }
     }
   }
 },
 "kibana-event-log-policy": {
   "version": 3,
   "modified_date": "2020-12-08T14:39:00.097Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb",
             "max_age": "30d"
           }
         }
       },
       "delete": {
         "min_age": "90d",
         "actions": {
           "delete": {
             "delete_searchable_snapshot": true
           }
         }
       }
     }
   }
 },
 "logs": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.227Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb",
             "max_age": "30d"
           }
         }
       }
     }
   }
 },
 "metricbeat": {
   "version": 2,
   "modified_date": "2021-01-15T14:02:42.090Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "30gb",
             "max_age": "1d"
           }
         }
       },
       "delete": {
         "min_age": "90d",
         "actions": {
           "wait_for_snapshot": {
             "policy": "mainbackuppolicy"
           }
         }
       }
     }
   }
 },
 "metrics": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.475Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb",
             "max_age": "30d"
           }
         }
       }
     }
   }
 },
 "ml-size-based-ilm-policy": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.083Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb"
           }
         }
       }
     }
   }
 },
 "slm-history-ilm-policy": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.585Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb",
             "max_age": "30d"
           }
         }
       },
       "delete": {
         "min_age": "90d",
         "actions": {
           "delete": {
             "delete_searchable_snapshot": true
           }
         }
       }
     }
   }
 },
 "synthetics": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.352Z",
   "policy": {
     "phases": {
       "hot": {
         "min_age": "0ms",
         "actions": {
           "rollover": {
             "max_size": "50gb",
             "max_age": "30d"
           }
         }
       }
     }
   }
 },
 "watch-history-ilm-policy": {
   "version": 1,
   "modified_date": "2020-12-08T14:31:15.792Z",
   "policy": {
     "phases": {
       "delete": {
         "min_age": "7d",
         "actions": {
           "delete": {
             "delete_searchable_snapshot": true
           }
         }
       }
     }
   }
 }
}

I do not know how I can map these JSON elements to the corresponding object since its name is variable e.g. filebeat, ilm-history-ilm-policy, etc.

I created the following Objects:

public class IndexLifeCycleManagement {
    private IndexLifeCycleManagementPolicy indexLifeCycleManagementPolicy;
}
public class IndexLifeCycleManagementPolicy {

    @JsonProperty("modified_date")
    private String modifiedDate;

    @JsonProperty("version")
    private int version;

    @JsonProperty("policy")
    private Policy policy;
}

But I think the problem lies in the fact that Jackson cannot map filebeat to a IndexLifecycleManagement-object

Thanks in advance Regards


Solution

  • Map each instance of IndexLifeCycleManagementPolicy with it's corresponding JSON attribute, like this:

    class IndexLifeCycleManagementPolicy{
        @JsonProperty("modified_date")
        private String modifiedDate;
    
        @JsonProperty("version")
        private int version;
    
        @JsonProperty("policy")
        private Policy policy;
    }
    
    class IndexLifeCycleManagement{
        @JsonProperty("ilm-history-ilm-policy")
        public IndexLifeCycleManagementPolicy ilmHistoryIlmPolicy;
    
        @JsonProperty("filebeat")
        public IndexLifeCycleManagementPolicy filebeat;
    
        // similarly for every other attribute at this level 
    }
    

    Edit1:
    You can use a Map of IndexLifeCycleManagementPolicy and set them dynamically through JsonAnySetter annotation like this:

    class IndexLifeCycleManagement{
    
        public Map<String, IndexLifeCycleManagementPolicy> getIndexLifeCycleManagementPolicyMap() {
            return indexLifeCycleManagementPolicyMap;
        }
    
        @JsonAnySetter
        public void setIndexLifeCycleManagementPolicyMap(String key, IndexLifeCycleManagementPolicy policy) {
            indexLifeCycleManagementPolicyMap.put(key, policy);
        }
    
        Map<String, IndexLifeCycleManagementPolicy> indexLifeCycleManagementPolicyMap = new HashMap<>();
    }
    

    For example: key = "filebeat" and value = IndexLifeCycleManagementPolicy instance.
    You can then have a getter to read this Map of IndexLifeCycleManagementPolicy instances.