skip to Main Content

I used the following long command to get a JSON value 7.3.9-win-x64.msi that I included below.

$ cat json.txt  | jq -r '.[] | .[] | .[] | .[].management | .[0].microsoft | .shell.associated.scripting.component.windows.powershell'

For JSON code it is:

{
    "PowerShell": {
        "is a task": {
            "automation": [
                {
                    "and": "configuration",
                    "management": [
                        {
                            "program": "from",
                            "microsoft": {
                                "consisting of": "a command-line",
                                "shell": {
                                    "and": "the",
                                    "associated": {
                                        "scripting": {
                                            "language.": "initially a windows",
                                            "component": {
                                                "only": "known as",
                                                "windows": {
                                                    "powershell": "7.3.9-win-x64.msi",
                                                    "it was made": "open-source"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    ]
                },
                {
                    "the former": "is built on the .NET Framework",
                    "the latter on": ".NET (previously .NET Core)."
                }
            ]
        }
    }
} 

I failed when I tried to apply several methods that I got from several articles on the internet to get the same value in a shorter way. So is there a more shortest command to do it?.

2

Answers


  1. If you’re willing to take the risk, you could go with s.t. like:

    .. | objects | .powershell // empty
    

    Or if you know s.t. distinctive about the string of interest, e.g. that it ends with ".msi":

    .. | strings | select(endswith(".msi"))
    
    Login or Signup to reply.
  2. If the full path must be specified along the lines of your attempt, you could slightly shorten it by skipping the explicit pipes:

    .[][][][].management[0].microsoft.shell.associated.scripting.component.windows.powershell
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search