Remove specific key from a JSON object using jq.
Pretty-print JSON sample.
$ jq . topics.json
{ "topics": [ { "topic": "cerberus" }, { "topic": "kraken" }, { "topic": "hydra" } ], "version": 1 }
Remove version
.
$ jq 'del(.version)' /tmp/topics.json
{ "topics": [ { "topic": "cerberus" }, { "topic": "kraken" }, { "topic": "hydra" } ] }
Remove specific topic
.
$ jq 'del(.topics[] | select(.topic=="hydra"))' /tmp/topics.json
{ "topics": [ { "topic": "cerberus" }, { "topic": "kraken" } ], "version": 1 }
Pretty-print JSON sample.
$ jq . topic.proposed.json
{ "version": 1, "partitions": [ { "topic": "cerberus", "partition": 0, "replicas": [ 1, 4, 2 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 1, "replicas": [ 2, 1, 3 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 2, "replicas": [ 3, 2, 4 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 3, "replicas": [ 4, 3, 1 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 4, "replicas": [ 1, 2, 3 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 5, "replicas": [ 2, 3, 4 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 6, "replicas": [ 3, 4, 1 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 7, "replicas": [ 4, 1, 2 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 8, "replicas": [ 1, 3, 4 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 9, "replicas": [ 2, 4, 1 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 10, "replicas": [ 3, 1, 2 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 11, "replicas": [ 4, 2, 3 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 12, "replicas": [ 1, 4, 2 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 13, "replicas": [ 2, 1, 3 ], "log_dirs": [ "any", "any", "any" ] }, { "topic": "cerberus", "partition": 14, "replicas": [ 3, 2, 4 ], "log_dirs": [ "any", "any", "any" ] } ] }
Remove log_dirs
from each partition.
$ jq 'del(.partitions[] | .log_dirs)' topic.proposed.json
{ "version": 1, "partitions": [ { "topic": "cerberus", "partition": 0, "replicas": [ 1, 4, 2 ] }, { "topic": "cerberus", "partition": 1, "replicas": [ 2, 1, 3 ] }, { "topic": "cerberus", "partition": 2, "replicas": [ 3, 2, 4 ] }, { "topic": "cerberus", "partition": 3, "replicas": [ 4, 3, 1 ] }, { "topic": "cerberus", "partition": 4, "replicas": [ 1, 2, 3 ] }, { "topic": "cerberus", "partition": 5, "replicas": [ 2, 3, 4 ] }, { "topic": "cerberus", "partition": 6, "replicas": [ 3, 4, 1 ] }, { "topic": "cerberus", "partition": 7, "replicas": [ 4, 1, 2 ] }, { "topic": "cerberus", "partition": 8, "replicas": [ 1, 3, 4 ] }, { "topic": "cerberus", "partition": 9, "replicas": [ 2, 4, 1 ] }, { "topic": "cerberus", "partition": 10, "replicas": [ 3, 1, 2 ] }, { "topic": "cerberus", "partition": 11, "replicas": [ 4, 2, 3 ] }, { "topic": "cerberus", "partition": 12, "replicas": [ 1, 4, 2 ] }, { "topic": "cerberus", "partition": 13, "replicas": [ 2, 1, 3 ] }, { "topic": "cerberus", "partition": 14, "replicas": [ 3, 2, 4 ] } ] }
Could not be simpler!