|
NAMEjo - JSON output from a shellSYNOPSISjo [-p] [-a] [-B] [-D] [-e] [-n] [-v] [-V] [-d keydelim] [-f file] [–] [ [-s|-n|-b] word ...]DESCRIPTIONjo creates a JSON string on stdout from words given it as arguments or read from stdin. If -f is specified, jo first loads the contents of file as a JSON object or array, then modifies it with subsequent words before printing the final JSON string to stdout. file may be specified as - to read from jo’s standard input; this takes precedence over reading words from stdin.Without option -a it generates an object whereby each word is a key=value (or key@value) pair with key being the JSON object element and value its value. jo attempts to guess the type of value in order to create number (using strtod(3)), string, or null values in JSON. A missing or empty value normally results in an element whose value is null. If -n is specified, this element is not created. jo normally treats key as a literal string value. If the -d option is specified, key will be interpreted as an object path, whose individual components are separated by the first character of keydelim. jo normally treats value as a literal string value, unless it begins with one of the following characters:
Escape the special character with a backslash to prevent this interpretation. jo treats key@value specifically as boolean JSON elements: if the value begins with T, t, or the numeric value is greater than zero, the result is true, else false. jo creates an array instead of an object when -a is specified. When the := operator is used in a word, the name to the right of := is a file containing JSON which is parsed and assigned to the key left of the operator. The file may be specified as - to read from jo’s standard input. TYPE COERCIONjo’s type guesses can be overridden on a per-word basis by prefixing word with -s for string, -n for number, or -b for boolean. The list of words must be prefixed with --, to indicate to jo that there are no more global options.Type coercion works as follows:
Coercing a non-number string to number outputs the length of the string. Coercing a non-boolean string to boolean outputs false if the string is empty, true otherwise. Type coercion only applies to key=value words, and individual words in a -a array. Coercing other words has no effect. EXAMPLESCreate an object. Note how the incorrectly-formatted float value becomes a string:
Pretty-print an array with a list of files in the current directory:
Create objects within objects; this works because if the first character of value is an open brace or a bracket we attempt to decode the remainder as JSON. Beware spaces in strings ...
Booleans as strings or as boolean (pay particular attention to switch; the -B option disables the default detection of the “true”, “false”, and “null” strings):
Elements (objects and arrays) can be nested. The following example nests an array called point and an object named geo:
The same example, using object paths:
Without -d, a different object is generated:
Create empty objects or arrays, intentionally or potentially:
Type coercion:
Read element values from files: a value which starts with @ is read in plain whereas if it begins with a % it will be base64-encoded and if it starts with : the contents are interpreted as JSON:
These characters can be escaped to avoid interpretation:
Read element values from a file in order to overcome ARG_MAX limits during object assignment:
Add elements to existing JSON:
Deduplicate object keys (jo appends duplicate object keys by default):
OPTIONSjo understands the following global options.
BUGSProbably.If a value given to jo expands to empty in the shell, then jo produces a null in object mode, and might appear to hang in array mode; it is not hanging, rather it’s reading stdin. This is not a bug. Numeric values are converted to numbers which can produce undesired results. If you quote a numeric value, jo will make it a string. Compare the following:
Omitting a closing bracket on a nested element causes a diagnostic message to print, but the output contains garbage anyway. This was designed thusly. RETURN CODESjo exits with a code 0 on success and non-zero on failure after indicating what caused the failure.AVAILABILITY<http://github.com/jpmens/jo>CREDITS
SEE ALSO
AUTHORJan-Piet Mens <http://jpmens.net>
Visit the GSP FreeBSD Man Page Interface. |