小编bkd*_*kdm的帖子

如何使用 jq 对输出进行排序/唯一

我有如下所示的 json:

% cat example.json
{
    "values" : [
        {
            "title": "B",
            "url": "https://B"
        },
        {
            "title": "A",
            "url": "https://A"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想根据标题对值进行排序。即预期输出

{
  "title": "A",
  "url": "https://A"
}
{
  "title": "B",
  "url": "https://B"
}
Run Code Online (Sandbox Code Playgroud)

尝试了一下打击。不起作用:

% jq '.values[] | sort' example.json           
jq: error (at example.json:12): object ({"title":"B...) cannot be sorted, as it is not an array

% jq '.values[] | sort_by(.title)' example.json
jq: error (at example.json:12): Cannot index string with string "title"
Run Code Online (Sandbox Code Playgroud)

sorting json unique jq

2
推荐指数
1
解决办法
3442
查看次数

标签 统计

jq ×1

json ×1

sorting ×1

unique ×1