Skip to content

Instantly share code, notes, and snippets.

@iinm
Created May 4, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iinm/b0c47c44e2604fb9333c699a1b2ebb14 to your computer and use it in GitHub Desktop.
Save iinm/b0c47c44e2604fb9333c699a1b2ebb14 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu -o pipefail
# given:
input=$(cat << JSON
{
"a": 1,
"b": { "c": 2 },
"d": { "e": [3, 4] }
}
JSON
)
# when:
out=$(
echo "$input" \
| jq --raw-output 'leaf_paths as $path | "\($path | join(".")):\(getpath($path))"' \
| sort \
| paste --serial --delimiters "\t"
)
# then:
test "$out" = "$(echo -e "a:1\tb.c:2\td.e.0:3\td.e.1:4")"
@iinm
Copy link
Author

iinm commented Feb 1, 2024

jq --raw-output 'paths(scalars) as $path | "\($path | join(".")):\(getpath($path))"'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment