Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq json filter and keep original structure

Tags:

json

select

edit

jq

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!

like image 947
Alessandro Perucchi Avatar asked Feb 26 '18 22:02

Alessandro Perucchi


1 Answers

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)

like image 67
RomanPerekhrest Avatar answered Nov 02 '22 07:11

RomanPerekhrest