Search code examples
javaspringspring-bootspring-restcontroller

How to avoid escape characters in Spring REST Controller response that returns list of JSON strings?


Use case: Return a list of JSON Strings from Spring Rest Controller (the JSON strings come from a third party library).

Problem: Response from REST Controller has escape characters. This happens only when the return type is List or array or any other collection type. Returning a single string works fine.
How to return list of JSON formatted strings but avoid the escape characters.

Code:

import java.util.Arrays;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("restjson")
public class RestJsonController {

    @GetMapping(value="list", produces = {MediaType.APPLICATION_JSON_VALUE})
    public List<String> getValues(){
        String value1 = "{\"name\":\"John\", \"age\":30}";
        String value2 = "{\"name\":\"Tom\", \"age\":21}";
        
        return Arrays.asList(value1, value2);
        //response has escape characters: 
        //["{\"name\":\"John\", \"age\":30}","{\"name\":\"Tom\", \"age\":21}"]
    }

    @GetMapping(value="single", produces = {MediaType.APPLICATION_JSON_VALUE})
    public String getValue(){
        String value1 = "{\"name\":\"John\", \"age\":30}";
        String value2 = "{\"name\":\"Tom\", \"age\":21}";
        
        return value1.concat(value2);
        //response has no escape characters: 
        //{"name":"John", "age":30}{"name":"Tom", "age":21}
    }
}

Springboot version: 2.7.0
Full code at: https://github.com/rai-sandeep/restjson/blob/main/src/main/java/com/sdprai/restjson/controller/RestJsonController.java

EDIT:
To avoid any confusion related to string concatenation, I have updated the code (see below). Returning a list even with just one JSON string results in escape characters in the response. But returning just a string does not have this problem. I don't understand the reason behind this difference. For my use case, is there a way to return a list of JSON strings without the escape characters?

import java.util.Collections;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("restjson")
public class RestJsonController {

    @GetMapping(value="list", produces = {MediaType.APPLICATION_JSON_VALUE})
    public List<String> getValues(){
        String value1 = "{\"name\":\"John\", \"age\":30}";
        
        return Collections.singletonList(value1);
        //returns: ["{\"name\":\"John\", \"age\":30}"]
    }

    @GetMapping(value="single", produces = {MediaType.APPLICATION_JSON_VALUE})
    public String getValue(){
        String value1 = "{\"name\":\"John\", \"age\":30}";
        
        return value1;
        //returns: {"name":"John", "age":30}
    }
}

Solution

  • You can try this to avoid escaping.

    @GetMapping(value="list", produces = {MediaType.APPLICATION_JSON_VALUE})
        public String getValues(){
            String value1 = "{\"name\":\"John\", \"age\":30}";
            String value2 = "{\"name\":\"Tom\", \"age\":21}";
            return Arrays.asList(value1, value2).toString();
        }
    

    Upvote & Accept if this works.