diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 8ab47defbe47075728235c0e1c06d4d69b46118f..61ae62eb8a9c61cbc4d2d2f6828c59ac1ece5fe6 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -419,7 +419,7 @@ json_lex_string(JsonLexContext *lex)
 	for (s = lex->token_start + 1; *s != '"'; ++s)
 	{
 		/* Per RFC4627, these characters MUST be escaped. */
-		if (*s < 32)
+		if ((unsigned char) *s < 32)
 		{
 			/* A NUL byte marks the (premature) end of the string. */
 			if (*s == '\0')
@@ -430,8 +430,8 @@ json_lex_string(JsonLexContext *lex)
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 					 errmsg("invalid input syntax for type json"),
-					 errdetail_internal("line %d: Character \"%c\" must be escaped.",
-						lex->line_number, *s)));
+					 errdetail_internal("line %d: Character with value \"0x%02x\" must be escaped.",
+						lex->line_number, (unsigned char) *s)));
 		}
 		else if (*s == '\\')
 		{
@@ -637,7 +637,7 @@ report_parse_error(JsonParseStack *stack, JsonLexContext *lex)
 			(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 			 errmsg("invalid input syntax for type json: \"%s\"",
 				lex->input),
- 			 errdetail_internal(detail, lex->line_number, token)));
+			 detail ? errdetail_internal(detail, lex->line_number, token) : 0));
 }
 
 /*
diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out
index ed8b237076270087f1e5a93c7adf0d8fd3e2d003..4b1ad89de6e6b73bdfcd3fb6fb2ef4dbbab0a3d5 100644
--- a/src/test/regress/expected/json.out
+++ b/src/test/regress/expected/json.out
@@ -26,8 +26,7 @@ def"'::json;					-- ERROR, unescaped newline in string constant
 ERROR:  invalid input syntax for type json
 LINE 1: SELECT '"abc
                ^
-DETAIL:  line 1: Character "
-" must be escaped.
+DETAIL:  line 1: Character with value "0x0a" must be escaped.
 SELECT '"\n\"\\"'::json;		-- OK, legal escapes
    json   
 ----------