Write jq output to a file

If a user wants to write jq output to a file, the solution is simple.

jq output to a file

Extract all project ids from the following json payload to a text file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "projects": [
    { 
      "id": 1 
    },
    { 
      "id": 2
    }
  ]
}
1
echo '{"projects": [{"id": 1}, {"id": 2}]}' | jq '.projects[].id' > ids.txt

Output: ids.txt:

1
2
1
2

Using the redirection operator (>), the jq results are written to a file.