-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json parser, and ignore lines that match with an empty key
- Loading branch information
Showing
7 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Json | ||
|
||
Syntax: `{json field expression}` | ||
|
||
Extract a JSON value based on the expression statement | ||
from [gjson](https://github.com/tidwall/gjson) | ||
|
||
When using, you likely often want to extract a specific bit of | ||
json from the line. If you want to match the entire line you | ||
will likely want to leave the match at the default `.*`, and | ||
provide `{0}` as the field. | ||
|
||
For example, this command would extract lastname from below: | ||
`rare filter -e '{json {0} "name.last"}` | ||
|
||
Example expressions from their documentation: | ||
|
||
```GJson Expressions | ||
"name.last" >> "Anderson" | ||
"age" >> 37 | ||
"children" >> ["Sara","Alex","Jack"] | ||
"children.#" >> 3 | ||
"children.1" >> "Alex" | ||
"child*.2" >> "Jack" | ||
"c?ildren.0" >> "Sara" | ||
"fav\.movie" >> "Deer Hunter" | ||
"friends.#.first" >> ["Dale","Roger","Jane"] | ||
"friends.1.last" >> "Craig" | ||
``` | ||
|
||
With the given example: | ||
|
||
```json | ||
{ | ||
"name": {"first": "Tom", "last": "Anderson"}, | ||
"age":37, | ||
"children": ["Sara","Alex","Jack"], | ||
"fav.movie": "Deer Hunter", | ||
"friends": [ | ||
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, | ||
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, | ||
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package expressions | ||
|
||
import "github.com/tidwall/gjson" | ||
|
||
func kfJson(args []KeyBuilderStage) KeyBuilderStage { | ||
if len(args) != 2 { | ||
return stageError(ErrorArgCount) | ||
} | ||
return KeyBuilderStage(func(context KeyBuilderContext) string { | ||
json := args[0](context) | ||
expression := args[1](context) | ||
return gjson.Get(json, expression).Str | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters