user2797894 user2797894 - 4 months ago 19
Java Question

Formatting issues with rest service json output

My below java code returns the following output.

public String getAttributeMaster_2() throws JsonProcessingException {
Cluster cluster = couchBaseConnectionManager.openBucketOfActivCluster(CouchbaseBucket.RangePlan);
Map<String,Object> attributeMap = new HashMap<String,Object>();
attributeMap.put("Product_Brand", attributeMasterRepository.getProductBrand(cluster, CouchbaseBucket.RangePlan));
attributeMap.put("Product_Type", attributeMasterRepository.getProductType(cluster, CouchbaseBucket.RangePlan));
attributeMap.put("Product_Event", attributeMasterRepository.getProductEvent(cluster, CouchbaseBucket.RangePlan));
String attribute = attributeMap.toString();

return attribute;

}


Output:

{Product_Type=[{"active":true,"description":"ACCESSORIES","id":1}, {"active":true,"description":"ALL IN ONES","id":2}], Product_Brand=[{"active":false,"description":"101 DALMATIANS","id":1}], Product_Event=[{"description":"BABY EVENT","id":2}, {"description":"ACTIVE EVENT","id":1}]}


Issue:
I want the category name in quotes.

For Ex: Product_Type as "Product_Type". Same is the case with Product_Brand and Product_Event.

Please let me know if someone can help me achieve this.

I am new to java coding. Appreciate your response.

Thanks,

Answer

I don't see the keys in attributeMap adding any value apart from generating the string representation returned by attributeMap.toString().

In this case, you can simply escape and add double quotes for the keys, like so:

Map<String,Object> attributeMap = new HashMap<String,Object>();
attributeMap.put("\"Product_Brand\"", ...);
attributeMap.put("\"Product_Type\"", ...);
attributeMap.put("\"Product_Event\"", ...);

String attribute = attributeMap.toString();