Using $ variables in jq from PowerShell

Viewed 9758

I am using this code:

jq --raw-output "(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv" output2.json > output3.csv

to create CSV from my JSON database:

[
  {
    "key1": "field1",
    "key2": "field2",
    "key3": "field3"
  },
  {
    "key1": "field4",
    "key2": "field5",
    "key3": "field6"
  }
]

The output is perfect on https://jqplay.org/, but doesn't seem to work at all when running on my command line. I use PowerShell inside VSCode.

I keep getting this error.

jq: error: syntax error, unexpected '|', expecting '$' or '[' or '{' (Windows cmd shell quoting issues?) at , 
line 1: (map(keys) | add | unique) as  | map(. as  |  | map([.])) as  | , [] | @csv
jq: 1 compile error

I guessed that the $ were the problem as they are not shown in the error text, so I tried escaping them in various ways but made no progress. I have searched extensively and no-one seems to have this problem, so I must be getting something very simple very wrong.

2 Answers
Related