Bibelo
Bibelo

Reputation: 231

Select all keys in sub-sub objects

I don't get how jq works. I'm trying hard but I just don't get it. See below how I now create a jq query.

So this is my JSON, and I just want to extract the name keys in all the sub-objects

{
  "checkpassword": {
    "checkpassword": {
      "containers": [
        {
          "name": "checkpassword",
          "exists": "true",
          "running": "true"
        }
      ],
      "projectdir": "true",
      "nginxdef": "true"
    }
  },
  "reverse_proxy": {
    "reverse_proxy": {
      "containers": [
        {
          "name": "reverse_proxy",
          "exists": "true",
          "running": "true"
        }
      ],
      "projectdir": "true"
    }
  }
}

I gave up trying by myself, I'm wasting time on it. I feel lots of users are in the same situation.

Please any help is appreciated.

.

NB: this is how I develop my queries:

root@priv  …/newProject   master  san_listProjects | jq -r '.[] | select( any(.name)'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
.[] | select( any(.name)
jq: 1 compile error
 root@priv  …/newProject   master  san_listProjects | jq -r '.[] | select( any(".name")'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
.[] | select( any(".name")
jq: 1 compile error
 root@priv  …/newProject   master  san_listProjects | jq -r '.[] | select( any("name")'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
.[] | select( any("name")
jq: 1 compile error
 root@priv  …/newProject   master  san_listProjects | jq -r '.[] | select(any("name")'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
.[] | select(any("name")
jq: 1 compile error
 root@priv  …/newProject   master  san_listProjects | jq -r '. | select(any("name")'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
. | select(any("name")
jq: 1 compile error
 root@priv  …/newProject   master  san_listProjects | jq -r '. | select(any(".name")'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
. | select(any(".name")
jq: 1 compile error
 root@priv  …/newProject   master  san_listProjects | jq -r '. | select(any(.name)'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
. | select(any(.name)
jq: 1 compile error

Upvotes: 1

Views: 261

Answers (1)

Inian
Inian

Reputation: 85825

The problem with your attempt is neither of . or .[] lets you access object keys nested at the level .name key is present. Also you have a missing close ) for all the invocations of select function.

There are multiple ways to get this done. One way is map the path leading up to the name key and get the value of that path

(paths | select( .[-1] == "name")) as $path | getpath($path)

jqplay Demo

or as noted in the comments, just use the recursive descent builtin recurse and select objects with fields containing .name key

recurse | select(has("name")?).name

Upvotes: 2

Related Questions