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

JQ在JSON中对整数值进行排序,json,jq,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"} {"Acc

我有一堆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"}
....
我试图根据请求的数量按升序对它们进行排序

但当我运行以下程序时:

jq-S'.results[]| map({(.field):.value})|添加'FILENAME | jq-S-c'排序依据(.u请求数)[]'

我的结局是:

{"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"}
...

基本上,sort_by函数将“558”/“59944”视为小于“8”、“6”等。是否有办法解决此问题?

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

jq -s -c 'sort_by(.number_of_requests|tonumber)'

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

jq -s -c 'sort_by(.number_of_requests|tonumber)'