How to Pretty‑Print JSON in Java Console Without Plugins
This article explains why JSON output in a Java console is often unreadable, presents a custom method that formats and indents JSON strings with visual markers, provides the full implementation code, shows the resulting output, and notes its current limitations.
When testing APIs, most data exchanges use JSON, but the default console representation is hard to read compared with browser extensions. After researching, the author created a utility method that formats JSON for console display.
The method output(JSONObject jsonObject) checks for null, converts the object to a string, replaces escaped slashes, and then iterates over each character to insert line breaks and indentation based on the nesting level. Special handling is added for commas, opening brackets, and closing brackets to maintain a clear hierarchical view.
/**
* 输出json
*
* @param jsonObject json格式响应实体
*/
public static JSONObject output(JSONObject jsonObject) {
if (MapUtils.isEmpty(jsonObject)) {
output("json 对象是空的!");
return jsonObject;
}
String start = SourceCode.getManyString(SPACE_1, 4);
String jsonStr = jsonObject.toString();// 先将json对象转化为string对象
jsonStr = jsonStr.replaceAll("\\/", OR);
int level = 0;// 用户标记层级
StringBuffer jsonResultStr = new StringBuffer("> ");
for (int i = 0; i < jsonStr.length(); i++) {
char piece = jsonStr.charAt(i);
if (i != 0 && '
' == jsonResultStr.charAt(jsonResultStr.length() - 1)) {
for (int k = 0; k < level; k++) {
jsonResultStr.append(start);
}
}
switch (piece) {
case ',':
char last = jsonStr.charAt(i - 1);
if ("\"0123456789le]}".contains(last + EMPTY)) {
jsonResultStr.append(piece + LINE);
} else {
jsonResultStr.append(piece);
}
break;
case '{':
case '[':
jsonResultStr.append(piece + LINE);
level++;
break;
case '}':
case ']':
jsonResultStr.append(LINE);
level--;
for (int k = 0; k < level; k++) {
jsonResultStr.append(start);
}
jsonResultStr.append(piece);
break;
default:
jsonResultStr.append(piece);
break;
}
}
output(LINE + "↘ ↘ ↘ ↘ ↘ ↘ ↘ ↘ json ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙" + LINE + jsonResultStr.toString().replaceAll(LINE, LINE + "> ") + LINE + "↘ ↘ ↘ ↘ ↘ ↘ ↘ ↘ json ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙");
return jsonObject;
}The formatted output is displayed with decorative arrows and indentation, making the JSON hierarchy obvious. An example screenshot is shown below.
Note that the method may misbehave when commas appear inside string values, when values are non‑string types such as numbers, null, or scientific notation, or when a value itself is a JSON string, as the parser could mistakenly treat it as a nested JSON object. Overall, the solution is functional and satisfactory for most debugging scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
