skip to Main Content

I have a object of type:

RestConsumerRequest<RewardsAccumulationRequest>

which has @JsonProperty annotated fields like:

@JsonProperty("product_code")
private String productCode;

@JsonProperty("sub_product_code")
private String subProductCode;

when I wrote it as String using:

LOG.info("RestConsumerRequest<RewardsAccumulationRequest> is {}", objectMapper.writeValueAsString(restRequest))

I got:

..., "product_code":"CSS","sub_product_code":"SC",...

I tried removing the backslashes with:

objectMapper.writeValueAsString(restRequest).replace("\","") 

the backlashes are still there.

If I use replaceAll:

LOG.info("RestConsumerRequest<RewardsAccumulationRequest> is {}", objectMapper.writeValueAsString(restRequest).replaceAll("\"", ""));

then all " are gone (without any quotes)

..., product_code:CSS,sub_product_code:SC,...

what is the proper way to remove the backslashes only, leaving properly quoted JSON? I guess there must be a smarter way to do this, such as some configuration for objectMapper.

2

Answers


  1. I have used below code, it printed as below.
    22:10:41.743 [main] INFO com.example.SpringBootDemo_3_3_0.JsonPrinting — {"product_code":"productCode","sub_product_code":"subProduct"}

    public class JsonPrinting {

    private static Logger logger = LoggerFactory.getLogger(JsonPrinting.class);
    
    public static void main(String[] args) throws JsonProcessingException {
    
        ObjectMapper objectMapper = new ObjectMapper();
        RewardsAccumulationRequest request = new RewardsAccumulationRequest("productCode", "subProduct");
        //System.out.println(objectMapper.writeValueAsString(request));
        logger.info(objectMapper.writeValueAsString(request));
    }
    

    }

    class RewardsAccumulationRequest implements Serializable {

    public RewardsAccumulationRequest(String productCode, String subProductCode) {
        this.productCode = productCode;
        this.subProductCode = subProductCode;
    }
    
    @JsonProperty("product_code")
    private String productCode;
    
    @JsonProperty("sub_product_code")
    private String subProductCode;
    

    }

    Login or Signup to reply.
  2. You don’t actually need to remove the backslashes because they aren’t part of the JSON itself—they’re just there to escape the quotes in the string when it’s printed out. If you want to see the JSON in a more readable format without all those backslashes, you can tweak the ObjectMapper to pretty-print the JSON for you.

    Here’s how you can do it:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    
    String jsonString = objectMapper.writeValueAsString(restRequest);
    LOG.info("RestConsumerRequest<RewardsAccumulationRequest> is {}", jsonString);
    

    and the output will be a prettier json :

    {
    "product_code": "CSS",
    "sub_product_code": "SC"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search