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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
JGrep is a command line tool and API for parsing JSON documents based on logical expressions.
### Installation:
jgrep is available as a gem:
gem install jgrep
### JGrep binary usage:
jgrep [expression] -i foo.json
or
cat "foo.json" | jgrep [expression]
### Flags:
-s, --simple [FIELDS] : Greps the JSON and only returns the value of the field(s) specified
-c, --compat : Returns the JSON in its non-pretty flat form
-n, --stream : Specify continuous input
-f, --flatten : Flatten the results as much as possible
-i, --input [FILENAME] : Target JSON file to use as input
-q, --quiet : Quiet; don't write to stdout. Exit with zero status if match found.
-v, --verbose : Verbose output that will list a document if it fails to parse
--start FIELD : Starts the grep at a specific key in the document
--slice [RANGE] : A range of the form 'n' or 'n..m', indicating which documents to extract from the final output
### Expressions:
JGrep uses the following logical symbols to define expressions.
'and' :
- [statement] and [statement]
Evaluates to true if both statements are true
'or' :
- [statement] and [statement]
Evaluates true if either statement is true
'not' :
- ! [statement]
- not [statement]
Inverts the value of statement
'+'
- +[value]
Returns true if value is present in the json document
'-'
- -[value]
Returns true if value is not present in the json doument
'(' and ')'
- (expression1) and expression2
Performs the operations inside the perentheses first.
### Statements:
A statement is defined as some value in a json document compared to another value.
Available comparison operators are '=', '<', '>', '<=', '>='
Examples:
foo.bar=1
foo.bar>0
foo.bar<=1.3
### Complex expressions:
Given a json document, {"foo":1, "bar":null}, the following are examples of valid expressions
Examples:
+foo
... returns true
-bar
... returns false
+foo and !(foo=2)
... returns true
!(foo>=2 and bar=null) or !(bar=null)
... returns true
### CLI missing an expression:
If JGrep is executed without a set expression, it will return an unmodified JSON document. The
-s flag can still be applied to the result.
### In document comparison:
If a document contains an array, the '[' and ']' operators can be used to define a comparison where
statements are checked for truth on a per element basis which will then be combined.
Example:
[foo.bar1=1 and foo.bar2=2]
on
[
{
"foo": [
{
"bar1":1
},
{
"bar2":2
}
]
},
{
"foo": [
{
"bar1":0
},
{
"bar2":0
}
]
}
]
will return
[
{
"foo": [
{
"bar1": 1
},
{
"bar2": 2
}
]
}
]
**Note**: In document comparison cannot be nested.
### The -s flag:
The s flag simplifies the output returned by JGrep. Given a JSON document
[{"a":1, "b":2, "c":3}, {"a":3, "b":2, "c":1}]
a JGrep invocation like
cat my.json | jgrep "a=1" -s b
will output
1
The s flag can also be used with multiple field, which will return JSON as output which only contain the specified fields.
**Note**: Separate fields by a space and enclose all fields in quotes (see example below)
Given:
[{"a":1, "b":2, "c":3}, {"a":3, "b":2, "c":1}]
a JGrep invocation like
cat my.json | jgrep "a>0" -s "a c"
will output
[
{
"a" : 1,
"c" : 3
},
{
"a" : 3,
"c" : 1
}
]
### The --start flag:
Some documents do not comply to our expected format, they might have an array embedded deep in a field. The --start
flag lets you pick a starting point for the grep.
An example document can be seen here:
{"results": [
{"name":"Jack", "surname":"Smith"},
{"name":"Jill", "surname":"Jones"}
]
}
This document does not comply to our standard but does contain data that can be searched - the _results_ field.
We can use the --start flat to tell jgrep to start looking for data in that field:
<pre>
$ cat my.json | jgrep --start results name=Jack -s surname
Smith
</pre>
### The --slice flag
Allows the user to provide an int or range to slice an array of
results with, in particular so a single element can be extracted, e.g.
$ echo '[{"foo": {"bar": "baz"}}, {"foo": {"bar":"baz"}}]' |
jgrep "foo.bar=baz" --slice 0
{
"foo": {
"bar": "baz"
}
}
### The --stream flag
With the --stream or -n flag, jgrep will process multiple JSON inputs (newline
separated) until standard input is closed. Each JSON input will be processed
as usual, but the output immediately printed.
### JGrep Gem usage:
require 'jgrep'
json = File.read("yourfile.json")
expression = "foo=1 or bar=1"
JGrep::jgrep(json, expression)
sflags = "foo"
JGrep::jgrep(json, expression, sflags)
|