2

Thank you for any help that you could provide.

I am trying to create a bash script to modify the value of a key from the settings.json file from Visual Studio Code.

The key that I want to modify is window.zoomLevel. I have tried to read it by using jq: jq -r '.window.zoomLevel' settings.json, jq -r 'window.zoomLevel' settings.json, or jq -r '.zoomLevel' settings.json, but I am not able to and I even get compile errors. I created a copy of settings.json and changed the name of the variable to window_zoomLevel and I was able to successfully read it with the same code: jq -r '.window_zoomLevel' settings.json. How can I access that variable without modifying the json file?

As you can imagine, I don't know much about json structures, so please help me out.

This is how the file looks:

{
    "security.workspace.trust.untrustedFiles": "open",
    "launch": {
        "configurations": [
        {
            "type": "bashdb",
            "request": "launch",
            "name": "Bash-Debug (hardcoded script name)",
            "cwd": "${/.../...}",
            "program": "$/.../....sh",
            "args": []
        }
        ]
    },
    "kite.showWelcomeNotificationOnStartup": false,
    "editor.fontSize": 10,
    "files.autoSave": "afterDelay",
    "screencastMode.fontSize": 20,
    "scm.inputFontSize": 10,
    "debug.console.fontSize": 10,
    "terminal.integrated.fontSize": 10,
    "markdown.preview.fontSize": 10,
    "window.zoomLevel": -1.5,
    "editor.mouseWheelZoom": true
}

1 Answer 1

2

I finally was able to read the variable and modified it with this code that I found here:

mv settings.json temp.json &&
jq -r '."window.zoomLevel" |= -1.5' temp.json > settings.json &&
rm temp.json

It was basically a problem with quotes and so on [."window.zoomLevel" vs '."window.zoomLevel"' (correct one)]. If anyone can explain this in further detail, I would appreciate it, but at least I was able to do what I wanted.

4
  • 2
    It's mentioned in the Basic Filters section of the documentation: "If the key contains special characters or starts with a digit, you need to surround it with double quotes like this: ."foo$", or else .["foo$"]" The period . is a special character. Commented Jun 1, 2022 at 0:45
  • Oh, thank you! I wasn't sure how to look for it and was feeling lost in the jq documentation, but it makes sense now.
    – Zaida
    Commented Jun 1, 2022 at 0:50
  • 1
    Put a && after each of the first 2 lines so you only execute the next line if the current one succeeds or you'll end up trashing your input file on any failure. The more common idiom, though, is cmd input > tmp && mv tmp input rather than mv input tmp && cmd tmp > input && rm tmp.
    – Ed Morton
    Commented Jun 1, 2022 at 12:33
  • Oh, thank you for that! I thought that if jq didn't parse correctly, then it would not modify the text, but I will add it to make sure that the file is not corrupted! Thank you!
    – Zaida
    Commented Jun 3, 2022 at 22:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .