https://github.com/NorthboundTrain/jqg
search JSON using JQ, printing full path/flattened results
https://github.com/NorthboundTrain/jqg
bash-script jq
Last synced: about 1 year ago
JSON representation
search JSON using JQ, printing full path/flattened results
- Host: GitHub
- URL: https://github.com/NorthboundTrain/jqg
- Owner: NorthboundTrain
- License: apache-2.0
- Created: 2021-05-02T16:24:22.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-08-27T19:13:45.000Z (almost 3 years ago)
- Last Synced: 2024-02-13T21:47:19.937Z (over 2 years ago)
- Topics: bash-script, jq
- Language: Shell
- Homepage:
- Size: 264 KB
- Stars: 19
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `jqg` - search, flatten, unflatten, and extract JSON using JQ
[](https://github.com/NorthboundTrain/jqg/actions/workflows/bats.yml)
[](https://raw.githubusercontent.com/NorthboundTrain/jqg/main/LICENSE)
[](https://github.com/NorthboundTrain/jqg/releases/latest)
[](https://semver.org/)
[](https://common-changelog.org)
[](https://github.com/RichardLitt/standard-readme)
JSON is an inherently hierarchical structure, which makes searching it for path information difficult. The JQG script flattens the hierarchical structure so that the path for each JSON end node is represented as a single string, thereby enabling easy searching and producing contextually meaningful results. It also produces valid JSON, which can be further processed, as needed.
For searching, JQG uses the [PCRE](https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions) engine built into JQ, which is much more powerful than `grep` or `egrep` (and it's certainly easier to use). For added flexibility, JQG can read from STDIN instead of from a file, allowing it to be used in pipelines, too. Finally, there are many options to control what is searched and how, as well as the format and content of the output.
Alternately, JQG can unflatten JSON that has been previously flattened (or structured to look that way) and it can also extract a subset of the JSON input, including the extraction terms in the results. Both of these alternatives can be combined with searching in a "composite mode". The convenience scripts `jqu` and `jqx` are provided to invoke those composite modes more easily.
###### example JSON used below: `odd-values.json`
```none
$ jq . odd-values.json
{
"one": {
"start-string": "foo",
"null-value": null,
"integer-number": 101,
"string-with-pipe": "this|that",
"key|with|pipe": true,
"string-with-parens": "(this and that)",
"key(with)parens": true,
"bare-parens()": true,
"left(paren-only": true,
"unmatched-left)-paren": false,
"dollar $ign": "both-sides-$now"
},
"two": [
{
"two-a": {
"non-integer-number": -101.75,
"number-zero": 0
},
"true-boolean": true,
"two-b": {
"false-boolean": false
}
},
{
"two-c": {
"alpha-num-1": "a1",
"alpha-num-2": "2b",
"alpha-num-3": "a12b"
}
}
],
"three": {
"empty-string": "",
"empty-object": {},
"empty-array": []
},
"four": [
"first",
null,
{},
999,
"fourth"
],
"end-string": "bar"
}
```
##### Search Mode
###### the old way: `jq` & `grep`
```none
# some not-too-useful jq/grep results
$ jq . odd-values.json | grep string
"start-string": "foo",
"string-with-pipe": "this|that",
"string-with-parens": "(this and that)",
"empty-string": "",
"end-string": "bar"
$ jq . odd-values.json | grep 0
"integer-number": 101,
"non-integer-number": -101.75,
"number-zero": 0
$ jq . odd-values.json | grep 'int\|false'
"integer-number": 101,
"unmatched-left)-paren": false,
"non-integer-number": -101.75,
"false-boolean": false
```
###### new and improved: `jqg`
```none
# much more useful jqg results
$ jqg string odd-values.json
{
"one.start-string": "foo",
"one.string-with-pipe": "this|that",
"one.string-with-parens": "(this and that)",
"three.empty-string": "",
"end-string": "bar"
}
$ jqg -v 0 odd-values.json
{
"one.integer-number": 101,
"two.0.two-a.non-integer-number": -101.75,
"two.0.two-a.number-zero": 0
}
$ jqg 'int|false' odd-values.json
{
"one.integer-number": 101,
"one.unmatched-left)-paren": false,
"two.0.two-a.non-integer-number": -101.75,
"two.0.two-b.false-boolean": false
}
# The power of PCRE
# - search values looking for a 0 without a preceding number
$ jqg -v '(?
© 2021 Joseph Casadonte