如何按 jq 中这些键的键和值对 json 文件进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30331504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:54:28  来源:igfitidea点击:

How to sort a json file by keys and values of those keys in jq

jsongitsortingjq

提问by karlos

We're building a website using the Pentaho CTools library, which has a graphical dashboard editor which writes out JSON-format files for part of the dashboard.

我们正在使用 Pentaho CTools 库构建一个网站,它有一个图形仪表板编辑器,可以为仪表板的一部分写出 JSON 格式的文件。

I'd like to apply a transform to these files before check-in to git in order to sort them by key and then by the value of certain keys. The purpose is to make diffs easier, since the editor has a habit of rearranging all of the json fields.

我想在签入到 git 之前对这些文件应用转换,以便按键然后按某些键的值对它们进行排序。目的是使差异更容易,因为编辑器习惯于重新排列所有 json 字段。

For example, we might have something like this:

例如,我们可能有这样的事情:

{
  "components": {
    "rows": [
      {
        "id": "CHARTS",
        "name": "Charts",
        "parent": "UnIqEiD",
        "properties": [
          {
            "name": "Group",
            "type": "Label",
            "value": "Charts"
          }
        ],
        "type": "Label",
        "typeDesc": "<i>Group</i>"
      },
      {
        "id": "kjalajsdjf",
        "meta_cdwSupport": "true",
        "parent": "CHARTS",
        "properties": [
          {
            "name": "name",
            "type": "Id",
            "value": "Value1"
          },
          {
            "name": "title",
            "type": "String",
            "value": "Value2"
          },
          {
            "name": "listeners",
            "type": "Listeners",
            "value": "[]"
          },
...

We are able to jq --sort-keys(http://stedolan.github.io/jq/) to sort all of the keys, but I'm struggling to find out how to use the sort_byfunction to then sort certain specific elements by the value of certain keys (so, in the example above, sorting by properties.namefor example. Any ideas?

我们能够jq --sort-keyshttp://stedolan.github.io/jq/)对所有键进行排序,但我正在努力找出如何使用该sort_by函数然后按某些键的值对某些特定元素进行排序(所以,在上面的例子中,例如排序properties.name。有什么想法吗?

回答by karlos

Ok with some assistance on the IRC channel I've found an answer.

好的,在 IRC 频道的帮助下,我找到了答案。

Basically, it looks like this:

基本上,它看起来像这样:

> jq '.components.rows|=sort_by(.id)|.components.rows[].properties|=sort_by(.name)' file.json > out.json

> jq '.components.rows|=sort_by(.id)|.components.rows[].properties|=sort_by(.name)' file.json > out.json

So you do the select of the right object, walking into arrays if needed, and then sort_by just takes a single value (I was trying sort_by(.components.rows.id)which failed).

所以你选择了正确的对象,如果需要的话进入数组,然后 sort_by 只取一个值(我正在尝试sort_by(.components.rows.id)失败)。

the |= instead of the | passes the values along instead of stripping them.

|= 而不是 | 传递值而不是剥离它们。

回答by Thibaud Ledent

If you are looking to sort the file by attributes, this command may be useful:

如果您希望按属性对文件进行排序,此命令可能很有用:

jq -S '.' "$file" > "$file"_sorted;