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!
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With