104

I have gitbash in Windows. I am trying to run jq but its giving me error.

$ ./jq-win64.exe 
jq 
parse error: Invalid numeric literal at line 2, column 0

Intention: I want to use jq to parse json.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
DotNetDeveloper
  • 1,051
  • 2
  • 7
  • 4
  • The error message you're seeing there is a side effect of running it in mintty. Reported [here](https://github.com/stedolan/jq/issues/1760). Running it with no arguments triggers it to read from stdin and it's trying to parse `jq` as the input. – Jeff Mercado Dec 30 '18 at 11:30
  • I'm having hard time doing this. I did everything mentioned in this question and when I run jq in my git bash I get to response. anyone could help me? – Souad Sep 27 '19 at 09:43

8 Answers8

141

Easiest solution and always latest version:


run this curl in your gitbash:

curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe

or manually save the jq-win64.exe in link above as jq.exe to your /usr/bin (which is in your git bash installation folder)

(if you are behind a proxy add the -x proxyhost:port)

enter image description here

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
  • 8
    I had problem with permissions from inside bash. I copied manually to C:\Program Files\Git\mingw64\bin – MorioBoncz Jan 31 '22 at 12:05
  • 11
    You need administrator rights to put to `/usr/bin/jq.exe` as this is in `Program Files`, so either save to local dir and then manually copy to `C:\Program Files\Git\usr\bin` or start Git Bash "as Administrator" – Gas Jun 03 '22 at 18:58
  • When a configuring a Windows gitlab runner to use bash, this wasn't enough. I _also_ had to add `C:\Program Files\Git\usr\bin` to the Windows system path, even though jq was found when typed into a normal git bash window. – M_M Jul 04 '23 at 12:40
87

Using jq-win64.exe from github.com/stedolan/jq/releases, I get

vonc@voncav MINGW64 /d/prgs/dl
$ ./jq-win64.exe --version
jq-1.6

vonc@voncav MINGW64 /d/prgs/dl
$ echo '{"foo": 0}' | ./jq-win64.exe .
{
  "foo": 0
}

So it does work, but it then depends on the json document you are parsing with it.
If that json document is not well-formed, that would generate the error you see.

In your bash session, you can define (or add to your ~/.bashrc) an alias:

alias jq=/path/to/jq-win64.exe

That way, you don't need to use ./jq, but directly jq.

$ echo '{"foo": 0}' | jq

In my case:

vonc@voncav:/$ alias jq=/mnt/d/dwnl/jq-win64.exe
vonc@voncav:/$ echo '{"foo": 0}' | jq
{
  "foo": 0
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, Von for the reply. How can we use just jq instead of full name - ./jq-win64.exe eg. echo '{"foo": 0}' | jq . Is it even possible from gitbash-windows. Thanks in advance. – DotNetDeveloper Dec 29 '18 at 23:31
  • @DotNetDeveloper Sure: use an alias. See my edited answer. – VonC Dec 30 '18 at 09:15
  • 1
    thank you, it worked like a charm.. alias is helpful for git bash.. – Santosh Kumar Arjunan Mar 13 '20 at 20:22
  • 1
    the cli hangs indefinitely after typing: `/my/path/to/jq-win64.exe` – kiltek May 07 '20 at 09:01
  • @kiltek Yes, that is expected: it needs some input. Without any input, it hangs, waiting to read anything from stdin. That is why the example use pipe: `echo '{"foo": 0}' | jq`. `jq` alone would indeed hang. – VonC May 07 '20 at 12:47
  • On my windows machine, I run `choco install jq`. When it finishes, I go back to my git bash terminal, without even restarting it, and `jq` is immediately available. – User51 May 18 '20 at 17:18
  • @User51 That would work too: I suppose the choco installation PATH is already in your USer Windows PATH (https://stackoverflow.com/a/28235975/6309) – VonC May 18 '20 at 18:19
  • i have alias `jq=/path/to/jq-win64.exe` and i can run the command from git bash. now i want to use jq in a .sh script that i run in git bash, but it says jq not found? How can i make it work in a bash script and not only from shell prompt? – chaixdev Feb 23 '21 at 11:07
  • 1
    @chaixdev in a script, you have a few alternatives to use your `jq` alias: https://askubuntu.com/a/98786/5470 – VonC Feb 23 '21 at 11:12
60

I hate answers that say you need another to use another tool to download, but using https://chocolatey.org/ made this incredibly simple for me.

From an elevated command-prompt, run:

choco install jq

jq magically works from bash going forward.

bryanbcook
  • 16,210
  • 2
  • 40
  • 69
  • 1
    It works like a charm! To install choco, I used below command in an elevated PowerShell: ```Set-ExecutionPolicy Bypass -Scope Process -Force; ` iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))``` – Thilina Ashen Gamage Jul 02 '20 at 05:50
  • Hi just wondering which path I should run this command? – wawawa Dec 09 '20 at 09:15
4

I just downloaded the binary to %HOMEPATH%/bin/jq-win64 and it worked right away via jq

$ mkdir -p $HOMEPATH/bin
$ curl -L -o $HOMEPATH/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe

test

$ jq --version
jq-1.6
alo Malbarez
  • 358
  • 2
  • 6
  • 16
kidbrax
  • 2,364
  • 3
  • 30
  • 38
4

Instead of using chocolatey (with elevated rights) you could also use scoop.sh:

scoop install jq
Marc vA
  • 51
  • 1
  • 5
2

Below steps worked for me in git bash on windows 10

curl -L -o jq-win64.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe

export PATH=$PATH:"/C/Users/devops/Downloads/jq-win64.exe

jq --version

jq-1.6

devops-admin
  • 1,447
  • 1
  • 15
  • 26
2

In my case, I have fixed it in Windows10 while practicing a scenario of "Techworld with Nana" course, as per the below steps:

1) open "cmd" prompt (Run as administrator)

2) curl -L -o /usr/bin/jq-win64.exe Download it here

3) You can add C:\Program Files\Git\usr\bin path to your environment variables. If you follow this step, ignore the below steps.

[OR]

3) Go to the directory where jq is downloaded: C:\Program Files\Git\usr\bin and copy the file to C:\ drive

4) Add the path C:\ to "path" environment variable. This PC-->Properties-->Advanced system settings --> Environment Variables -->Edit-->New. Then add the path (where you copied jq.exe, i.e., C:) and Save it.

5) Now open a new "cmd" prompt, jq --version

Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
1

For windows Powershell && cmd

Download executable -> jq 1+ executables for 64-bit

Create folder in Program Files -> JQ

Copy .exe into program folder

Set environment variable to that folder

Restart terminal

Set alias

 Set-Alias -Name jq -Value .\jq-win64.exe

Run command

 echo '{"foo": 0}' | jq 

Output

 {
    "foo": 0
 }

enjoy!

Christian Matthew
  • 4,014
  • 4
  • 33
  • 43