0
votes

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)

1
Your English is fine, but in future, please follow the minimal reproducible example guidelines more closely. In particular, a valid JSON snippet would be appropriate here. Thanks. - peak
You need quotes around $data in the for statement (and note that it's not an array) and around $var in the jq statement. - Dennis Williamson

1 Answers

0
votes

To perform the selection based on .id, you could use the technique exemplified in the following:

jq --argjson id 10 -f select.jq <<< "$var"

where select.jq contains:

.results[] | select( .id == $id )

Using --argjson here avoids having to convert a string to a number.

Since the jq filter here is so short, you might want to include the filter on the command-line instead of putting it in a file.