@antti5/jgrep
v0.2.0
Published
Grep-like tool for JSON
Readme
jgrep
This is a simple command-line tool that searches JSON files with a syntax very similar to grep.
Instead of matching the regular expression against the file content, the expression is matched against the key path of the traversed JSON content. Values whose key paths match the expression are printed out.
Install globally with: npm install --global @antti5/jgrep
Example
$ jgrep repository.url package.json
git+https://github.com/antti5/cmd-line-args.gitCompressed files
To help working with large files, the tool has built-in support for files compressed with Gzip, XZ or Brotli. These files are decompressed on the fly based on the file extension.
Gzip and XZ files are also decompressed when read from stdin, but for Brotli this is not possible because it does not have a magic signature.
JSON files with comments
If the input file has extension .jsonc then it can contain C-style comments i.e. either /* */ block comments or // line comments. These comments are removed before the JSON is parsed.
Options
| Option | Description | | --- | ----------- | | -k, --key | Instead of printing the property value, print the matched key | | -p, --path | Instead of printing the property value, print the matched key path | | -f, --file | Print only the file name if there is a match | | -q, --quiet | Print nothing, only indicate matches with process exit value | | -i, --ignoreCase | Use case-insensitive matching | | -V, --matchValue | Instead of matching the property key path, match property values | | -o, --one | Print only one match for each input file | | -I, --indent | Print matched object values indented on multiple lines | | -U, --unique | Print each unique property value only once for each input file | | -E, --enclosing | Print the enclosing object of the matching property | | -L, --lookup | When property keys are printed, property keys are looked up from files listed in environment variable JGREP_ID_FILES |
The option -L or --lookup is intended for cases where the JSON file uses property keys that are not human-readable. In this case mapping can be provided with CSV-formatted files where
each line contains an identifier and a human-readable value separated by a semicolon ;. For example:
123;John
456;Mary
789;FidoSeveral such lookup files can be provided, and their filenames can be set in the environment variable JGREP_ID_FILES delimited with semicolons.
