1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# jgrep
A grep-like tool for searching JSON files by matching values against patterns.
## Installation
```bash
# Install system-wide
sudo make install
# Or specify prefix
make install PREFIX=~/.local
# Uninstall
sudo make uninstall
```
## Usage
```
jgrep [-e|-w] PATTERN [-p LEVEL] [-o] [--full] [-i] [-k KEY] [-K RE] [-P RE] [FILE...]
```
Options and file arguments can be interspersed in any order.
## Options
| Option | Description |
|--------|-------------|
| `-e, --expr REGEX` | Regular expression pattern (default) |
| `-w, --fixed TEXT` | Plain text search (no regex) |
| `-p, --parent LEVEL` | Parent levels to traverse up (default: 0 = value) |
| `-o, --only-matching` | Output only the matched value (same as -p 0) |
| `--full` | Output full context: {key, value, parent, path} |
| `-i, --ignore-case` | Case insensitive matching |
| `-k, --key KEY` | Only search values under this key (exact match) |
| `-K, --key-expr RE` | Only search values under keys matching regex |
| `-P, --path-expr RE` | Only match paths matching regex (joined by .) |
| `-h, --help` | Show help |
## Examples
```bash
# Basic search - return matched values (default)
jgrep 'error' logs.json
# Get parent objects containing match
jgrep -p 1 '@test\.org' users.json
# Full context output for custom formatting
jgrep --full 'error' logs.json | jq '{key: .key, val: .value}'
# Filter by key name (exact)
jgrep -k email '@test' users.json
# Filter by key name (regex)
jgrep -K 'email|name' 'john' users.json
# Filter by path
jgrep -P 'employees\.\d+\.email' '@test' data.json
# Regex search - emails starting with 'j'
jgrep -e '^j.*@' users.json
# Plain text search - literal dots not treated as regex
jgrep -w 'config.json' data.json
# Get grandparent (2 levels up)
jgrep 'needle' -p 2 nested.json
# Case insensitive search
jgrep -i 'ERROR' logs.json
# Search multiple files
jgrep 'TODO' *.json
# Read from stdin
curl -s api.example.com/data | jgrep 'status'
```
## How It Works
jgrep recursively searches all string values in JSON structures. By default it returns matched values. Use `-p` to traverse up to parent objects.
```bash
# Given nested.json:
# {"departments": [{"name": "eng", "employees": [{"email": "a@test.org"}]}]}
jgrep 'test.org' nested.json
# "a@test.org"
jgrep 'test.org' -p 1 nested.json
# {"email": "a@test.org"}
jgrep --full 'test.org' nested.json
# {"key":"email","value":"a@test.org","path":[...],"parent":"a@test.org"}
jgrep -P 'departments\.0' '@' nested.json
# Only matches in first department
```
## Dependencies
- bash
- jq
## License
MIT
|