An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# `jqg` - search, flatten, unflatten, and extract JSON using JQ

[![BATS](https://github.com/NorthboundTrain/jqg/actions/workflows/bats.yml/badge.svg)](https://github.com/NorthboundTrain/jqg/actions/workflows/bats.yml)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-yellow.svg)](https://raw.githubusercontent.com/NorthboundTrain/jqg/main/LICENSE)
[![Latest Release](https://img.shields.io/github/v/release/NorthboundTrain/jqg?sort=semver)](https://github.com/NorthboundTrain/jqg/releases/latest)
[![Semantic Versioning](https://img.shields.io/badge/semantic_versioning-grey)](https://semver.org/)
[![Common Changelog](https://common-changelog.org/badge.svg)](https://common-changelog.org)
[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](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