Search code examples
javajsonjacksonbackslash

Create JSON backslash + slash with Jackson library


In Java class there is a field

    String cardExpiration = "1022"; //I can format it as needed

Using Jackson libray I need to create the following JSON:

{
  "cardExpiration":"10\/22"
}

I'm using the following code

        String cardExpiration = "10\\/22";
        MyPojo myPojo = new MyPojo(cardExpiration);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(myPojo);

This code returns JSON with "10\\/22" (double backslashes, but I need just one backslash). I can not create in Java String "10\/22", as it is illegal escape character in string literal. Any idea how to get in JSON "10\/22"?


Solution

  • Escape the backslash with a second one to define it as String.

    String cardExpiration = "10\\/22";
    System.out.println(cardExpiration); // prints 10\/22
    

    Here a working example that includes the usage of Jackson for serializing your POJO. Note that Jackson escapes the Backslash inside the JSON auto. for you. If you really want to have the JSON value {"cardExpiration":10\/22} you can add to the attribute in the POJO the @JsonRawValue annotation.

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.11.2</version>
            </dependency>
        </dependencies>
    </project>
    

    App.java

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class App {
    
        public static void main(String[] args) throws JsonProcessingException {
            String cardExpiration = "10\\/22";
            System.out.println(cardExpiration); // prints 10\/22
    
            MyPojo myPojo = new MyPojo(cardExpiration);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(myPojo);
            System.out.println(json); // prints {"cardExpiration":"10\\/22"}
        }
        
    }
    
    class MyPojo {
        String cardExpiration;
    
        public MyPojo(String cardExpiration) {
            this.cardExpiration = cardExpiration;
        }
    
        public String getCardExpiration() {
            return cardExpiration;
        }
    
        public void setCardExpiration(String cardExpiration) {
            this.cardExpiration = cardExpiration;
        }
    }