6

I am really new to the command jq, and I am trying do some filtering to remove blocks of data that I don't want/need.

Here is an example of my JSON structure:

{
  "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 * * *"
        }
      ]
    }
  ]
}

I want to filter this in order to have only the "type": "filesystem", and get the following output:

{
  "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 * * *"
        }
      ]
    }
  ]
}

I have try some commands like

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

But it destroys the original structure.

I have searched around, and found lots of example, but lots doesn't work, or doesn't give me what I need.

Has someone any ideas? If someone has also any good learning website in order to understand jq, that would be awesome!

Thanks in advance!

peak
  • 105,803
  • 17
  • 152
  • 177
  • 1
    Would you mind simplifying your question by creating a "Minimal, Complete, and Verifiable example". I do appreciate the question very much, but I don't want to read your entire json object structure to make sense of it! – markling Feb 23 '21 at 11:28

1 Answers1

8

jq solution:

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

The output:

{
  "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)

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105