JQ 对 JSON 中的整数值进行排序

Gau*_*ose 1 json jq

我有一堆 JSON 对象:

{"AccountID":"290859614811","number_of_requests":"59944"}
{"AccountID":"421258792169","number_of_requests":"3132"}
{"AccountID":"433594311540","number_of_requests":"1541"}
{"AccountID":"406912498377","number_of_requests":"678"}
{"AccountID":"850981987534","number_of_requests":"558"}
{"AccountID":"763725063017","number_of_requests":"470"}
{"AccountID":"notaccount","number_of_requests":"8"}
....
Run Code Online (Sandbox Code Playgroud)

我试图根据请求数按升序对它们进行排序。

但是当我运行以下命令时:

jq -S '.results[] | map( { (.field) : .value} ) | add ' FILENAME | jq -s -c 'sort_by(.number_of_requests)[]'

我最终得到:

{"AccountID":"433594311540","number_of_requests":"1541"}
{"AccountID":"421258792169","number_of_requests":"3132"}
{"AccountID":"763725063017","number_of_requests":"470"}
{"AccountID":"850981987534","number_of_requests":"558"}
{"AccountID":"290859614811","number_of_requests":"59944"}
{"AccountID":"406912498377","number_of_requests":"678"}
{"AccountID":"notaccount","number_of_requests":"8"}
...
Run Code Online (Sandbox Code Playgroud)

基本上, sort_by 函数将 "558"/"59944" 视为小于 "8" 、 "6" 等。有没有办法解决这个问题?

ric*_*ici 6

您需要将值转换为数字,使用tonumber:

jq -s -c 'sort_by(.number_of_requests|tonumber)'
Run Code Online (Sandbox Code Playgroud)