I have a Json Format as a Template (Temp.json). Below is the format of my Template
{
"products":[
{
"ProductTitleName": "",
"ImageUrl":""
}
]
}
Now I have to Populate Data to this format, basically products array will have many object Nodes. I have used JsonPath expressions to extract relevant attribute value from Raw Json.My Problem is how can I use this Template and Populate data to this structure.
Reason to use Template Json
-
so to avoid those mentioned reasons, I planned to have a Template json and adhere to that structure, is this a good approach, if so help me out to populate data to the template, if not help me out with a better approach.
If you need only to avoid pojo classes you can use a generic Map
to do the same. In this case you don't need to parse the basic template.
Map<String, Object> products = new HashMap<>();
List<Map<String, Object>> listProducts = new ArrayList<>();
for () { // Loop over products
Map<String, Object> product = new HashMap<>();
product.put("ProductTitleName", "YourTitle");
product.put("ImageUrl", "YourImageUrl");
listProducts.add(product);
}
products.put("products", listProducts);
// Now you can use products Map instead of an equivalent pojo class
If you need to use a template try to use Velocity. It is a template engine that works well to build text file (in this case a json file), but from a template. It is not limited to files, it works also with strings or streams if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With