Jq - find workspace.id from partial title?

I am finding jq totally impenetrable! This is the JSON data:

hyprctl -j clients                                                  
[{
    "address": "0x63663778ab60",
    "mapped": true,
    "hidden": false,
    "at": [360, -1798],
    "size": [1199, 539],
    "workspace": {
        "id": -98,
        "name": "special:scratch_term"
    },
    "floating": true,
    "monitor": 0,
    "class": "kitty-dropterm",
    "title": "~",
    "initialClass": "kitty-dropterm",
    "initialTitle": "zsh",
    "pid": 80720,
    "xwayland": false,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": 4
},{
    "address": "0x6366377a6ea0",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x6366377a8490",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x6366377a9a80",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x6366377a4930",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x636637756370",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x636631f095c0",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x6366377bc3e0",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x63663780e3c0",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x636638002280",
    "mapped": false,
    "hidden": false,
    "at": [0, 0],
    "size": [0, 0],
    "workspace": {
        "id": -1,
        "name": ""
    },
    "floating": false,
    "monitor": -1,
    "class": "",
    "title": "",
    "initialClass": "",
    "initialTitle": "",
    "pid": -1,
    "xwayland": true,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": -1
},{
    "address": "0x636637810900",
    "mapped": true,
    "hidden": false,
    "at": [824, 4],
    "size": [771, 891],
    "workspace": {
        "id": 2,
        "name": "2"
    },
    "floating": false,
    "monitor": 0,
    "class": "kitty",
    "title": "~",
    "initialClass": "kitty",
    "initialTitle": "zsh",
    "pid": 122864,
    "xwayland": false,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": 3
},{
    "address": "0x6366377fcd60",
    "mapped": true,
    "hidden": false,
    "at": [44, 644],
    "size": [1551, 251],
    "workspace": {
        "id": 1,
        "name": "1"
    },
    "floating": false,
    "monitor": 0,
    "class": "kitty",
    "title": "…/.config/hypr/config",
    "initialClass": "kitty",
    "initialTitle": "zsh",
    "pid": 138379,
    "xwayland": false,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": 0
},{
    "address": "0x636638020de0",
    "mapped": true,
    "hidden": false,
    "at": [44, 4],
    "size": [1551, 632],
    "workspace": {
        "id": 1,
        "name": "1"
    },
    "floating": false,
    "monitor": 0,
    "class": "kitty",
    "title": "mi ../scripts/hypr-brow.sh",
    "initialClass": "kitty",
    "initialTitle": "zsh",
    "pid": 103632,
    "xwayland": false,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": 2
},{
    "address": "0x6366377d99a0",
    "mapped": true,
    "hidden": false,
    "at": [44, 4],
    "size": [1551, 891],
    "workspace": {
        "id": 3,
        "name": "3"
    },
    "floating": false,
    "monitor": 0,
    "class": "",
    "title": "EndeavourOS - A terminal-centric distro with a vibrant and friendly community at its core. - Opera",
    "initialClass": "",
    "initialTitle": "Loading… - Opera",
    "pid": 164873,
    "xwayland": false,
    "pinned": false,
    "fullscreen": false,
    "fullscreenMode": 0,
    "fakeFullscreen": false,
    "grouped": [],
    "swallowing": "0x0",
    "focusHistoryID": 1
}]

This is what I have got:

hyprctl -j clients | jq '.[] | .title | select(.|test("^.*Opera$"))'

Which returns:

"EndeavourOS - A terminal-centric distro with a vibrant and friendly community at its core. - Opera"

How do I get Opera’s workspace ID? Currently I am feeding the title into another jq:

hyprctl -j clients | jq -r '.[] | select(.title == $title) | .workspace.id'

Which seems a bit long winded… Of course if Opera set it’s bloody class I would not have this problem!!

 jq '.[] | "\(.workspace.id) \(.title)"'
1 Like

Problem with your query is that you are selecting just title strings, and then matching them to your regex.
You need to select the full client object that matches, and then filter to workspace id, so it should be:

hyprctl -j clients | jq '.[] | select(.title|test("^.*Opera$")) | .workspace.id'
2 Likes

Thanks, will try this when I get home tonight.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.