I'm trying to see if there is a way to format a json array string with a single %s, without the backslashes. The API I'm working on attaches this formatted string as a body parameter and sends it over to the server. When the server gets it, there are backslahes in the string. I would need this string to be without the escape characters.
The constant looks like this:
final String REQUEST_PARAMS = "{\"params\":{\"details\":{\"param1\":\"%s\",\"param2\":\"%s\",\"extraParams\":\"%s\"}}}";
This is the snippet for converting the json array to string:
...
ObjectMapper mapper = new ObjectMapper();
String extraParams = mapper.writeValueAsString(params.getExtraParams()); // this looks like: [{"hostValue": "id1", "portNumber": "name1"}, {...}] for example.
String formattedParams = String.format(REQUEST_PARAMS, "param1", "param2", extraParams); // {"params":{"details":{"param1":"param1","param2":"param2","extraParams":"[{"hostValue": "id1", "portNumber": "name1"}]"}}}
...
However, when this gets sent over to the backend API, it looks like:
{"params":{"details":{"param1":"param1","param2":"param2","extraParams":[{\"hostValue\": \"id1\", \"portNumber\": \"name1\"}]}}}.
If I format a string by specifying having %s for each field in the json array, it works without the backslashes (e.g hostValue: %s, portNumber: %s). However, this wont work since there is a possibility that there is an unknown parameter in the json array that is not predefined in the constant (e.g sqlPORTNumber).
I want to make sure there is no escape characters being sent over to the server, is there a way to achieve this? Any insights would be greatly appreciated!
Don’t use String.format. It is not the right tool for this. It will not cover the various needs of well-formed JSON syntax. Using String.format will eventually result in invalid JSON.
Use your ObjectMapper to convert data to JSON:
Instead of creating three Maps, a better design is to create bean classes: