0Pricing
Java Academy · Lesson

Use Cases: JSON and SQL

Embed structured text cleanly.

Where Text Blocks Shine

Text blocks are perfect for embedding structured text like JSON, SQL, HTML, and XML. These formats use lots of quotes and line breaks that are painful with ordinary strings.

Embedding JSON

JSON is full of double quotes. In a text block they need no escaping, so the literal looks exactly like real JSON.

public class Main {
    public static void main(String[] args) {
        String json = """
            {
              "name": "Alice",
              "age": 30,
              "active": true
            }
            """;
        System.out.print(json);
    }
}

All lessons in this course

  1. Declaring Text Blocks
  2. Indentation and Stripping
  3. Escapes in Text Blocks
  4. Use Cases: JSON and SQL
← Back to Java Academy