0Pricing
Java Academy · Lesson

Use Cases: JSON and SQL

Embed structured text cleanly.

Use Cases: JSON and SQL is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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);
    }
}

JSON Arrays

Nested arrays and objects stay readable because the indentation matches the real structure.

public class Main {
    public static void main(String[] args) {
        String json = """
            {
              "colors": ["red", "green", "blue"]
            }
            """;
        System.out.print(json);
    }
}

Embedding SQL

Multi-line SQL queries become clear and copy-pasteable. No more concatenating fragments with stray spaces.

public class Main {
    public static void main(String[] args) {
        String query = """
            SELECT id, name
            FROM users
            WHERE active = true
            ORDER BY name
            """;
        System.out.print(query);
    }
}

SQL With Quoted Values

SQL string literals use single quotes, which never conflict with the text block delimiter, so they pass through unchanged.

public class Main {
    public static void main(String[] args) {
        String sql = """
            SELECT * FROM products
            WHERE category = 'books'
            """;
        System.out.print(sql);
    }
}

Building Dynamic Values

To inject runtime values, use String.format with placeholders. Avoid manual SQL concatenation in real apps, but for display this is clear.

public class Main {
    public static void main(String[] args) {
        String template = """
            SELECT * FROM users
            WHERE id = %d
            """;
        String query = String.format(template, 42);
        System.out.print(query);
    }
}

The formatted Method

Text blocks pair nicely with the instance method formatted(...), which is shorthand for String.format applied to the block.

public class Main {
    public static void main(String[] args) {
        String json = """
            {"user": "%s", "score": %d}
            """.formatted("Bob", 95);
        System.out.print(json);
    }
}

Embedding HTML

HTML markup with attributes uses many double quotes. Text blocks keep it tidy and valid-looking.

public class Main {
    public static void main(String[] args) {
        String html = """
            <a href="https://example.com" class="link">
              Click here
            </a>
            """;
        System.out.print(html);
    }
}

Embedding XML

XML configuration snippets are another great fit, preserving the hierarchy at a glance.

public class Main {
    public static void main(String[] args) {
        String xml = """
            <config>
              <port>8080</port>
              <debug>true</debug>
            </config>
            """;
        System.out.print(xml);
    }
}

Processing Embedded Data

Once defined, treat the block like any string. Here we count the rows of a CSV-style block.

public class Main {
    public static void main(String[] args) {
        String csv = """
            id,name
            1,Alice
            2,Bob
            """;
        String[] rows = csv.strip().split("\n");
        System.out.println("Data rows: " + (rows.length - 1));
    }
}

Combining Multiple Snippets

You can build larger documents by formatting smaller text blocks and joining them.

public class Main {
    public static void main(String[] args) {
        String row = """
            <li>%s</li>
            """;
        String list = "<ul>\n" + row.formatted("One") + row.formatted("Two") + "</ul>";
        System.out.print(list);
    }
}

Quick Check

Why are text blocks especially convenient for embedding JSON?

Recap

You learned practical text block use cases:

  • Great for JSON, SQL, HTML, XML
  • Double quotes need no escaping
  • Use formatted(...) or String.format to inject values
  • The result is a normal String you can parse or split
  • Indentation mirrors the real structure

Frequently asked questions

Is the “Use Cases: JSON and SQL” lesson free?

Yes — the full text of “Use Cases: JSON and SQL” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Use Cases: JSON and SQL”?

Embed structured text cleanly. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Use Cases: JSON and SQL” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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