My problem is as follows: I get a json file from the output of a get request and put it in a variable. What I put in this variable is a results table containing objects. Each object has the same format. For example, they often have keys like 'id' and 'name'. What I would like to extract for example, will be the value for each id that is an integer.
On the other hand, it's not my ultimate goal. It's only one step. Even if that's not my question, I'm going to present this one to you to contextualize it. What I'm looking for, is that according to a key and a value, the program returns the object whose specified key contains the value. I hope I've made myself clear.
Thank you for your help !
I think problems can come from bad input which is not a JSON format but a string. This is some examples of what I tried :
1) test="$(./Downloads/jq-win64.exe -ncR '.id' $var)"
2) test="$(./Downloads/jq-win64.exe '.id' $var)"
3) test="$(./Downloads/jq-win64.exe '.id' JSON.parse($var))"
4) test="$(./Downloads/jq-win64.exe '.id' <<< $var)"
#!/bin/bash
data="$(curl -H "Accept: application/json; indent=4" https://private-ip.net/api/path | ./Downloads/jq-win64.exe '.results')"
for var in ${data[*]}
do
test="$(./Downloads/jq-win64.exe '.id' <<< $var)"
echo "$test"
done
Input : { "count": 19, "next": null, "previous": null, "results": [ { "id": 10, "name": "XXXXXXXXXXXXXXXXXXX", "rd": "XXXXXXXXXXX", "tenant": XXXXXXXXXXX, "enforce_unique": XXXXXXXXXXX, "description": "XXXXXXXXXXX", "tags": [], "display_name": "XXXXXXXXXXX", "custom_fields": XXXXXXXXXXX, "created": "XXXXXXXXXXX", "last_updated": "XXXXXXXXXXX" }, { "id": 11, "name": "XXXXXXXXXXX", "rd": "XXXXXXXXXXX", "tenant": XXXXXXXXXXX, "enforce_unique": XXXXXXXXXXX, "description": "XXXXXXXXXXX", "tags": XXXXXXXXXXX, "display_name": "XXXXXXXXXXX", "custom_fields": XXXXXXXXXXX, "created": "XXXXXXXXXXX", "last_updated": "XXXXXXXXXXX" }, . .. ... }
Output - Results expected (with id for instance): 10 11 5 6 4 ...
Actual results foreach:
1) null (infinite loop)
2) Invalid argumentot open file 10, jq: error: Could not open file "name":: Invalid argument (infinite loop with different key)
3) ./filtering.sh: command substitution: line 11: syntax error near unexpected token ('
./filtering.sh: command substitution: line 11:./Downloads/jq-win64.exe '.id' JSON.parse($var))"'
4) jq: error (at :1): Cannot index string with string "id" parse error: Expected string key before ':' at line 1, column 15 (infinite loop with different line/column number)
$datain theforstatement (and note that it's not an array) and around$varin thejqstatement. - Dennis Williamson