Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a JSON request body as a text block and explain how incidental indentation and the closing delimiter position affect the result.

A text block starts with three double quotes followed by a line break, so its content begins on the next line. The compiler then removes incidental indentation: the smallest common leading whitespace of all non-blank content lines and of the closing delimiter's line, so the closing delimiter's position controls how much indentation stays. Moving it left keeps more indentation, putting it at the end of the last content line removes the trailing newline, line endings become \n, trailing spaces are stripped unless we escape them with \s, and a backslash at the end of a line joins it with the next.

Why this question is asked

Text blocks, standard since Java 15, are the natural way to write JSON, SQL or HTML inside Java code, especially in tests. They are easy to read, but the rules for indentation and newlines are precise, and getting them wrong produces a request body with unexpected spaces or a missing final newline that makes an exact-match test fail. Interviewers ask this to see whether we know those rules rather than guessing. In this answer, we will write a JSON body as a text block, see how the compiler decides what indentation is incidental, move the closing delimiter around to see the effect, and cover newlines, trailing spaces and line joining, before answering how to insert values safely.

1A Quick Recap of Text Block Syntax

A text block opens with """ and the rest of that line must be empty apart from whitespace; the content starts on the following line. It closes with another """. Inside, we can write double quotes freely, which is what makes JSON so much easier to read than an escaped ordinary string literal.

Notice that a one-line text block is not allowed. Writing """ {"name": "a"} """ on a single line is a compile error, because the opening delimiter must be followed by a line terminator. For short JSON, an ordinary string literal is fine.

2Writing the JSON Body

Here is a request body inside a method, indented to match the surrounding code. The comment after it shows the exact string, with each newline written as \n.

Java
String json = """
{
"name": "a"
}
""";
// result: {\n "name": "a"\n}\n
String old = "{\n \"name\": \"a\"\n}\n"; // the same string as an ordinary literal
System.out.println(json.equals(old)); // true

The eight spaces in front of every line are not part of the string. They are incidental indentation, there only because the code is indented. The two spaces before "name" are kept, because they are part of the JSON's own layout. So how does the compiler tell the two apart?

3How the Compiler Removes Incidental Indentation

The compiler looks at every content line that is not blank, plus the line holding the closing delimiter, and counts the leading whitespace of each. It takes the smallest of those counts and removes that many characters from the start of every line. In our example, the lines { and } have 8 spaces, the "name" line has 10, and the closing """ line has 8, so the minimum is 8, and 8 spaces are removed from each line.

Step throughHow a text block becomes a string
  1. Take the lines after the opening """the opening line itself adds nothing
  2. Normalise line endingsCR LF and CR become LF (\n)
  3. Find the minimum indentationnon-blank lines and the closing delimiter line
  4. Strip that many leading spaces from each line
  5. Strip trailing spaces on each lineunless escaped with \s
  6. Interpret escapes\n, \t, \", \s, backslash-newline joins lines

6 steps in order. Play them, or step through them one at a time.

Escapes are interpreted last, so an escaped \s or \n is never stripped.

Notice the role of the closing delimiter's line. It is counted like a content line, which gives us a simple control over how much indentation is kept.

4Moving the Closing Delimiter

If we move the closing delimiter four columns to the left, the minimum indentation becomes 4, so only 4 spaces are removed and every line keeps 4 extra. If we put the closing delimiter right after the last content line, it no longer has a line of its own, so it is not counted, and there is no final line terminator, so the string has no trailing newline.

Java
String moreIndent = """
{
"name": "a"
}
""";
// result: ____{\n______"name": "a"\n____}\n (each _ is a space: 4 extra on every line)
String noNewline = """
{
"name": "a"
}""";
// result: {\n "name": "a"\n} (no trailing \n)

Moving the delimiter to the right of the content does not add indentation; the content lines still set the minimum. For JSON sent over HTTP, the trailing newline usually does not matter, but for tests that compare exact strings, or for signed payloads, it does, so it pays to choose deliberately.

5Newlines, Trailing Spaces and Joining Lines

Line terminators in a text block are always normalised to \n, whatever the source file uses, so a file edited on Windows produces the same string as one edited on Linux. Trailing spaces at the end of each line are removed, because editors add and remove them invisibly. If we really need one, we write \s, which is an escape for a single space and is processed after stripping, so it survives. A backslash at the very end of a line removes the line break, which lets us split a long line in the source without splitting it in the string.

Java
String text = """
one \
two
three\s
""";
// result: one two\nthree \n (the backslash joins the first two lines; \s keeps the final space)

Notice that the space before the backslash on the first line is kept, because it is not trailing whitespace once the line is joined. These three rules, normalised \n, stripped trailing spaces and backslash joining, are what make text blocks stable across editors and operating systems.

6Follow-up questions

How would you insert values into a text block safely?

For values we control, such as numbers in a test, String.formatted() works: we write a text block containing "name": "%s" and call .formatted(name) on it. But formatted() does no escaping, so a name containing a double quote or a backslash produces invalid JSON, and untrusted input can inject extra fields. For real request bodies, build the object and let a JSON library such as Jackson serialise it, which escapes correctly. String templates, which would have added safe interpolation, were previewed in JDK 21 and 22 and then withdrawn, so check your version rather than relying on them.

Are text blocks a different type at run time?

No. A text block is just another way to write a String literal. The result is an ordinary String, interned like other literals, and the compiler does all the processing, so there is no run-time cost for stripping indentation.

What does stripIndent() do?

String.stripIndent() applies the same incidental-indentation algorithm to a String at run time, and translateEscapes() applies the escape processing. They are useful when text comes from a file rather than from source code, but most code never needs them.

7Conclusion

In this answer, we wrote a JSON body as a text block and saw that its content starts after the opening delimiter's line break, that the compiler removes the smallest common indentation of the non-blank lines and the closing delimiter's line, and that the closing delimiter's position therefore controls how much indentation stays and whether there is a trailing newline. We also saw that line endings become \n, trailing spaces are stripped unless written as \s, and a backslash at the end of a line joins lines. For inserting values, formatted() is fine for trusted values, but real JSON should be built by a JSON library.

What to say in the interviewAbout 100 seconds aloud

Open with

I would write it with three quotes, a line break, then the JSON lines indented to match my code, and the closing three quotes on their own line at the same indentation as the braces.

Then, if they want more

The content starts on the line after the opening delimiter; you cannot put a text block on one line. The compiler then strips incidental indentation: it looks at all the non-blank content lines plus the closing delimiter's line, takes the smallest leading whitespace, and removes that much from every line.

So with the braces and the closing delimiter at eight spaces and the name line at ten, I get the brace at column zero, the name line indented by two, and a trailing newline. The closing delimiter is the control. If I move it left, say to four spaces, every line keeps four extra spaces.

If I put it right after the closing brace on the same line, it does not count for indentation and there is no trailing newline, which matters for exact-match tests.

A few other rules: line endings are normalised to backslash n regardless of the source file, trailing spaces are stripped unless I write backslash s, and a backslash at the end of a line joins it with the next.

For inserting values I would use formatted only for trusted test values, since it does no escaping, and for real payloads I would serialise an object with Jackson so quotes and backslashes are escaped properly.

About this answer

Last updated
12 November 2026
Versions it names
Java 15, Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a JSON request body as a text block and explain how incidental indentation and the...": https://scrivio-interview-library.netlify.app/java/strings/77babf822e/