Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq select() and sort_by()

Tags:

json

curl

jq

api

I am parsing a curl output from gitlab api, and I need to add a sort_by to my query, then select only certain values.

sample input:

[
  {
    "id": 10,
    "name": "another-test",
    "path": "another-test",
    "description": "",
    "visibility": "private",
    "lfs_enabled": true,
    "avatar_url": null,
    "web_url": "https://mygitlab/groups/another-test",
    "request_access_enabled": false,
    "full_name": "another-test",
    "full_path": "another-test",
    "parent_id": 9
  },
  {
    "id": 11,
    "name": "asdfg",
    "path": "asdfg",
    "description": "",
    "visibility": "private",
    "lfs_enabled": true,
    "avatar_url": null,
    "web_url": "https://mygitlab/groups/asdfg",
    "request_access_enabled": false,
    "full_name": "asdfg",
    "full_path": "asdfg",
    "parent_id": 7
  }

I parse the JSON with jq as follows:

curl http://..... | jq -r '.[] | select(.parent_id!=null) | .name, .parent_id' 

This works exactly as expected, but when I try to sort the results by parent_id, I get an error:

curl http://..... | jq -r '.[] | select(.parent_id!=null) | .name, .parent_id | sort_by(.parent_id)'
jq: error (at <stdin>:0): Cannot index number with string "parent_id"

I can use sort_by(), by putting a single dot instead than .[]:

curl http://..... | jq '. | sort_by(.parent_id) '

But I cannot combine the 2 functions.

Clarification: I need to extract name and parent_id, sorted by parent_id, when it is not null.

Thanks in advance

like image 252
Bruno9779 Avatar asked Mar 09 '18 18:03

Bruno9779


People also ask

How do you sort output in jq?

Sorting JSON by value with jq can easily be done by using the sort_by() function. The main trick with using the sort_by() function is that your JSON input must be in an array. There are different ways to do this in jq (if your data is not already like this), including using the -s or --slurp option.

What is jq slurp?

“Slurp” tells jq to read every line of the input JSON lines and treat the entire group as one huge array of objects. With the Twitter data still in the input box on jq play, check the “Slurp” box, and just put . in the filter.

What is jq query?

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed , awk , grep and friends let you play with text. jq is written in portable C, and it has zero runtime dependencies.

What is a jq filter?

A jq program is a "filter": it takes an input, and produces an output. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks.


1 Answers

jq's sort_by() function accepts an array as input.

curl 'http://...' |
    jq -r '
        map(select(.parent_id != null)) 
        | sort_by(.parent_id)[]
        | [.name, .parent_id]
        | @tsv
    '

Sample output:

asdfg   7
another-test    9
like image 61
RomanPerekhrest Avatar answered Nov 15 '22 21:11

RomanPerekhrest