jq json 过滤并保持原始结构

jq json filter and keep original structure

我对命令 jq 真的很陌生,我正在尝试进行一些过滤以删除我不需要的数据块 want/need。

这是我的 JSON 结构的示例:

{
  "BackupCfg": [
    {
      "type": "filesystem",
      "repository": "trunk",
      "url": "test.example.com",
      "port": "394",
      "cfg": [
        {
          "Default": "true",
          "ID": "trunk00",
          "Paths": [
            "/etc",
            "/home",
            "/var",
            "/usr/local",
            "/opt",
            "/root"
          ],
          "Cron": "33 0 * * *"
        }
      ]
    },
    {
      "type": "filesystem",
      "repository": "trunk02",
      "url": "test.example.com",
      "port": "394",
      "cfg": [
        {
          "ID": "trunk01",
          "Paths": [
            "/opt/example",
            "/opt/var_example"
          ],
          "Cron": "*/30 0-23 * * *"
        }
      ]
    },
    {
      "type": "database",
      "repository": "trunk-db",
      "url": "test.example.com",
      "port": "399",
      "cfg": [
        {
          "Default": "true",
          "ID": "trunk00",
          "db_type": "mysql",
          "db_hostname": "localhost",
          "db_port": "3306",
          "db_user": "root",
          "db_pwd": "password",
          "databases": [],
          "Cron": "40 0 * * *"
        },
        {
          "ID": "trunk01",
          "db_type": "mysql",
          "db_hostname": "localhost",
          "db_port": "3307",
          "db_user": "riit",
          "db_pwd": "passwird",
          "databases": [],
          "Cron": "33 3 * * *"
        },
        {
          "Default": "false",
          "ID": "trunk02",
          "db_type": "postgres",
          "db_hostname": "localhost",
          "db_port": "3308",
          "db_user": "ruut",
          "db_pwd": "passwurd",
          "databases": [],
          "Cron": "0 10 * * *"
        }
      ]
    }
  ]
}

我想过滤它以便只有 "type":"filesystem",并获得以下输出:

{
  "BackupCfg": [
    {
      "type": "filesystem",
      "repository": "trunk",
      "url": "test.example.com",
      "port": "394",
      "cfg": [
        {
          "Default": "true",
          "ID": "trunk00",
          "Paths": [
            "/etc",
            "/home",
            "/var",
            "/usr/local",
            "/opt",
            "/root"
          ],
          "Cron": "33 0 * * *"
        }
      ]
    },
    {
      "type": "filesystem",
      "repository": "trunk02",
      "url": "test.example.com",
      "port": "394",
      "cfg": [
        {
          "ID": "trunk01",
          "Paths": [
            "/opt/example",
            "/opt/var_example"
          ],
          "Cron": "*/30 0-23 * * *"
        }
      ]
    }
  ]
}

我尝试了一些命令,比如

jq  '.[][] | select(.type | contains("filesystem"))'

但是破坏了原来的结构

我四处搜索,找到了很多示例,但很多都不起作用,或者没有提供我需要的东西。

有人有什么想法吗? 如果谁也有什么好的学习网站来了解jq,那就太棒了!

提前致谢!

jq解法:

jq '.BackupCfg |= map(select(.type == "filesystem"))' file.json

输出:

{
  "BackupCfg": [
    {
      "type": "filesystem",
      "repository": "trunk",
      "url": "test.example.com",
      "port": "394",
      "cfg": [
        {
          "Default": "true",
          "ID": "trunk00",
          "Paths": [
            "/etc",
            "/home",
            "/var",
            "/usr/local",
            "/opt",
            "/root"
          ],
          "Cron": "33 0 * * *"
        }
      ]
    },
    {
      "type": "filesystem",
      "repository": "trunk02",
      "url": "test.example.com",
      "port": "394",
      "cfg": [
        {
          "ID": "trunk01",
          "Paths": [
            "/opt/example",
            "/opt/var_example"
          ],
          "Cron": "*/30 0-23 * * *"
        }
      ]
    }
  ]
}

https://stedolan.github.io/jq/manual/v1.5/#select(boolean_expression)