Unsure how to translate from jq/jello: not-equals test, get multiple values from each of multiple maps #146
-
[{"name": "click", "version": "7.1.2", "latest_version": "8.0.1", "latest_filetype": "wheel"}, {"name": "decorator", "version": "4.4.2", "latest_version": "5.0.9", "latest_filetype": "wheel"}, {"name": "ipython", "version": "7.20.0", "latest_version": "7.25.0", "latest_filetype": "wheel"}, {"name": "pandas", "version": "1.3.0", "latest_version": "1.3.1", "latest_filetype": "wheel"}, {"name": "parso", "version": "0.8.1", "latest_version": "0.8.2", "latest_filetype": "wheel"}, {"name": "pip", "version": "21.1.3", "latest_version": "21.2.1", "latest_filetype": "wheel"}, {"name": "prompt-toolkit", "version": "3.0.14", "latest_version": "3.0.19", "latest_filetype": "wheel"}, {"name": "Pygments", "version": "2.7.4", "latest_version": "2.9.0", "latest_filetype": "wheel"}, {"name": "setuptools", "version": "49.2.1", "latest_version": "57.4.0", "latest_filetype": "wheel"}, {"name": "tomli", "version": "1.0.4", "latest_version": "1.1.0", "latest_filetype": "wheel"}] In English, I'd like to get space or line separated output for each map in the list that does not have a name on a blocklist ( $ jq -r '.[] | select(.name|test("^(setuptools|six|pip|pip-tools)$")|not) | .name,.version,.latest_version' <test.json
click
7.1.2
8.0.1
decorator
4.4.2
5.0.9
ipython
7.20.0
7.25.0
pandas
1.3.0
1.3.1
parso
0.8.1
0.8.2
prompt-toolkit
3.0.14
3.0.19
Pygments
2.7.4
2.9.0
tomli
1.0.4
1.1.0
$ jello -r '" ".join(" ".join((pkg["name"], pkg["version"], pkg["latest_version"])) for pkg in _ if pkg["name"] not in ("setuptools", "six", "pip", "pip-tools"))' <test.json
click 7.1.2 8.0.1 decorator 4.4.2 5.0.9 ipython 7.20.0 7.25.0 pandas 1.3.0 1.3.1 parso 0.8.1 0.8.2 prompt-toolkit 3.0.14 3.0.19 Pygments 2.7.4 2.9.0 tomli 1.0.4 1.1.0 While those two outputs differ in whitespace, either is suitable for my purposes. My closest (but obviously wrong) attempt: $ dasel select --plain -f test.json -p json -m '.(.name=setuptools).name'
setuptools Thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
As of dasel -f test.json -m
--format '{{ .name }}{{ newline }}{{ .version }}{{ newline }}{{ .latest_version }}'
'.(name!=setuptools)(name!=six)(name!=pip)(name!=pip-tools)' This uses a multi select to target the items to be included in the result and then uses the format flag to give the desired output. The related documentation can be found here: https://daseldocs.tomwright.me/usage/flags/format Hey @AndydeCleyre, thanks for raising this discussion it's highlighted some areas than dasel is lacking features as of now. Currently there are 2 issues here:
Comparison operatorIn order to add a negative comparison operator I will simply have to do this work for you. This means instead of this: You will be able to combine many dynamic selectors to make something like this:
Output formattingAlthough not a capability provided by dasel, you could easily use a bash loop or something to get the formatting you need. dasel -f test.json --compact -m '.(name!=setuptools)(name!=six)(name!=pip)(name!=pip-tools)' | while read line; do
echo "$line" | dasel -p json --plain .name
echo "$line" | dasel -p json --plain .version
echo "$line" | dasel -p json --plain .latest_version
done In general I've been holding off from adding anything that can be achieved easily with bash or something similar, but if there's enough interest I'd be happy to add some formatting capabilities in the future. |
Beta Was this translation helpful? Give feedback.
As of
v1.18.0
you can do this with the following command:This uses a multi select to target the items to be included in the result and then uses the format flag to give the desired output.
The related documentation can be found here: https://daseldocs.tomwright.me/usage/flags/format
Hey @AndydeCleyre, thanks for raising this discussion it's highlighted some areas than dasel is lacking features as of now.
Currently there are 2 issues here: