user1763590
user1763590

Reputation: 137

jq assignment where the path contain space

This might be a simple question but I am struggling to figure out how jq work with spaces in elements. (The following is in windows powershell)

PS C:\Users\Home> "{}" | jq ".mytest.path = """"success""""" 
{
  "mytest": {
    "path": "success"
  }
}

Now, I want to make "my test" two separate words. My desired output is

{
  "my - test": {
    "path": "success"
  }
}

What query would I used? I tried the following:

PS C:\Users\Home> "{}" | jq ".""my - test"".path = """"success""""" 
jq : jq: error: Could not open file test.path = "success": Invalid argument
At line:1 char:8
+ "{}" | jq ".""my - test"".path = """"success"""""
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (jq: error: Coul...nvalid argument:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS C:\Users\Home> "{}" | jq --raw-output ".""my test"".path = """"success""""" 
jq : jq: error: Could not open file test.path = "success": Invalid argument
At line:1 char:8
+ "{}" | jq --raw-output ".""my test"".path = """"success"""""
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (jq: error: Coul...nvalid argument:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Any suggestions?

Upvotes: 0

Views: 252

Answers (1)

peak
peak

Reputation: 116880

First, setting aside for the moment all the issues related to peculiarities of different shells, the jq filter you need is:

.["my test"].path = "success"

(If ever in doubt about using the .foo syntax, you can always fall back on the fundamental form: .["key name"].)

Since you are evidently conversant with the transmogrification required by the mighty PowerShell, I'll just point out here that one can simply place the jq program in a file (say program.jq) and invoke jq with the -f option (jq -f program.jq ...).

Upvotes: 1

Related Questions