check-for-json-errors-in-doc failing for escaped characters
After making the change in #3287 (closed) and specifically !2299 (merged) to pass all shell scripts to shellcheck, and then making the required changes to appease shellcheck, one of the scripts (./tools/check-for-json-errors-in-doc.sh
) and its associated CI job (check-for-json-errors-in-doc
) started failing under certain conditions.
The problem is that characters which are double escaped (as they should be in a well formed JSON) are being resolved by the echo command and result in a single-escaped character in the JSON that is being checked which results in malformed JSON.
One solution is to use printf
instead of echo
to not resolve the double escaped characters or any to no resolve any other element for that matter.
Example which can be replicated locally on Debian 12. Notice the escaped comma:
$ ./tools/check-for-json-errors-in-doc.sh doc/sphinx/arm/dhcp4-srv.rst
processing: doc/sphinx/arm/dhcp4-srv.rst
parse error: Invalid escape at line 10, column 49
file doc/sphinx/arm/dhcp4-srv.rst contains invalid JSON near line 1730
===start of JSON block===
{
"subnet4": [
{
"pools": [
{
"option-data": [
{
"name": "boot-file-name",
"data": "foo\,bar"
}
]
}
]
}
]
}
====end of JSON block====
parse error: Invalid escape at line 7, column 103
file doc/sphinx/arm/dhcp4-srv.rst contains invalid JSON near line 4641
===start of JSON block===
{
"name": "v4-dnr",
"data": "2, resolver.example., 10.0.5.6, alpn=dot\,doq port=8530 | 3, fooexp.resolver.example."
}
====end of JSON block====
EXIT 1