P
P
puma342021-10-05 16:44:32
JSON
puma34, 2021-10-05 16:44:32

Remove from jq keys containing part of a word?

There is a flat json how to use jq to remove from it all keys in the name of which there is the word pass

{
"description": "",
"hostname": "ubuntu18",
"ip": "",
"password_1": "test123",
" password_2": "test1231",
"username_1": "test",
"username_1_owner": "dude1",
"username_2": "test1",
"username_2_owner": "dude2"
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2021-10-09
@puma34

$ jq 'with_entries(select(.key|test("password")|not))' config.json
{
  "description": "",
  "hostname": "ubuntu18",
  "ip": "",
  "username_1": "test",
  "username_1_owner": "dude1",
  "username_2": "test1",
  "username_2_owner": "dude2"
}

S
Stalker_RED, 2021-10-05
@Stalker_RED

Object.keys(obj).forEach(key => {
  if (key.includes('pass')) {
    delete obj[key];
  }
});

And jq is not needed here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question