json#
Small helpers to escape strings and extract fields from JSON.
Functions#
- json_escape input
- Purpose: Escape backslashes, quotes, and control characters for JSON string context.
- Behavior:
\and"are backslash-escaped; newline, carriage return and tab become\n,\r,\t; every other character in U+0001-U+001F becomes\u00XX. Printed withprintf '%s\n', so inputs such as-nare output as-is. -
Returns: escaped string.
-
format_response json
- Purpose: Validate JSON and print its
.responsefield. -
Behavior: Exits non-zero (and logs error) if the input is not valid JSON or is empty.
-
format_md_response text
-
Purpose: Strip triple backticks from a response blob for markdown-friendly output.
-
json_get_field BODY KEY
- Purpose: Print a top-level scalar field of a JSON object. Strings print raw; numbers and booleans print in JSON spelling (
true,false). - Behavior: uses
python3when present; without it, agrep/sedfallback reads string fields from compact or pretty JSON ("version"never matches"api_version"). An absent field, an object or list value, or a body that is not JSON prints nothing and returns 0. - Returns: 1 only when
KEYis not[A-Za-z0-9_]+.
Dependencies#
jqfor JSON validation/extraction (format_response);python3optional forjson_get_field.