Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write output to a file after piped to jq

How can I write a piped output that went through jq to a file in shell

Example:

curl api.example.com | jq > call.txt 

won't work. Neither does

(curl api.example.com | jq) > call.txt 

Help!

Edit: So doing curl api.example.com > call.txt works just fine. So it has to do with piping it to jq

like image 841
supersize Avatar asked Feb 24 '18 15:02

supersize


People also ask

Can jq write JSON?

The safest way to create JSON on the command line is through using a tool that constructs it for you as jq does.

What is pipe jq?

Different jq commands are “piped” to JSON output. The simplest command ( | jq ) pretty prints the JSON output, which makes the JSON more readable. This command saves you time because you no longer need to copy the output and format it in a text editor just to make it more readable.

What is jq in curl?

jq is a program described as “ sed for JSON data": You can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.


1 Answers

Just calling jq without a filter will throw errors if stdout isn't a terminal

$ curl https://jsonplaceholder.typicode.com/posts/1 | jq > test.txt jq - commandline JSON processor [version 1.5-1-a5b5cbe] Usage: jq [options] <jq filter> [file...]          jq is a tool for processing JSON inputs, applying the         given filter to its JSON text inputs and producing the [...] 

Try jq '.' (i.e: pretty-print the input JSON):

$ curl https://jsonplaceholder.typicode.com/posts/1 | jq '.' > test.txt   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                  Dload  Upload   Total   Spent    Left  Speed 100   292  100   292    0     0   1698      0 --:--:-- --:--:-- --:--:--  1707 

Note that the filter is not really optional:

From man jq:

JQ(1)                                                                                JQ(1)  NAME        jq - Command-line JSON processor  SYNOPSIS        jq [options...] filter [files...] 

According to the tip of the master branch... your described (and my observed) behaviour is not expected...

Older versions of jq have the following: (here)

if (!program && isatty(STDOUT_FILENO) && !isatty(STDIN_FILENO))   program = "."; 

i.e: use a default filter if stdout is a TTY, and stdin is not a TTY.

This behaviour appears to be corrected in commit 5fe05367, with the following snippet of code:

if (!program && (!isatty(STDOUT_FILENO) || !isatty(STDIN_FILENO)))   program = "."; 
like image 197
Attie Avatar answered Sep 23 '22 01:09

Attie