4

I have a curl command

 curl -H "Accept: application/json" https://icanhazdadjoke.com/

Which returns the JSON (note: I chose this api because it has no auth so everyone can help test, it returns a formatted json but most API's return a flat json with no formatting... One line)

 {
  "id": "5wAIRfaaUvc", 
  "joke": "What do you do when a blonde throws a grenade at you? Pull the pin and throw it back.", 
  "status": 200
}

When I pipe to JQ, jq responds as expected. I pipe to jq to ensure I have a formatted readable json

curl -H "Accept: application/json" https://icanhazdadjoke.com/ | jq

Returns

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   110  100   110    0     0    320      0 --:--:-- --:--:-- --:--:--   321
{
  "id": "NCAIYLeNe",
  "joke": "I fear for the calendar, it’s days are numbered.",
  "status": 200
}

BUT when I pipe the output of JQ to a text file (I want a formatted version to be saved for readability, not the plain unformatted json)I get an error

curl -H "Accept: application/json" https://icanhazdadjoke.com/ | jq > file.txt

Returns

jq - commandline JSON processor [version 1.5]
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
    filter's results as JSON on standard output.
    The simplest filter is ., which is the identity filter,
    copying jq's input to its output unmodified (except for
    formatting).
    For more advanced filters see the jq(1) manpage ("man jq")
    and/or https://stedolan.github.io/jq

    Some of the options include:
     -c     compact instead of pretty-printed output;
     -n     use `null` as the single input value;
     -e     set the exit status code based on the output;
     -s     read (slurp) all inputs into an array; apply filter to it;
     -r     output raw strings, not JSON texts;
     -R     read raw strings, not JSON texts;
     -C     colorize JSON;
     -M     monochrome (don't colorize JSON);
     -S     sort keys of objects on output;
     --tab  use tabs for indentation;
     --arg a v  set variable $a to value <v>;
     --argjson a v  set variable $a to JSON value <v>;
     --slurpfile a f    set variable $a to an array of JSON texts read from <f>;
    See the manpage for more options.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   141  100   141    0     0    317      0 --:--:-- --:--:-- --:--:--   316
(23) Failed writing body
4
  • That said, the output from icanhazdadjoke doesn't actually return as one line. You're showing it as several lines in the question, and I get it over several lines (already pretty-printed) when I run it myself. Commented Nov 30, 2017 at 16:45
  • Yes, if you read the note section of my question I say that this API does return pretty print. I am working on an authed api that does not, figure an API with no auth better for stackoverflow Commented Nov 30, 2017 at 16:47
  • Gotcha. I appreciate your efforts to make a reproducer others could use. For future note, ix.io are sprunge.us are both ad-free pastebins with documented curl-friendly interfaces by which one can pipe a file and let folks get the exact same bits back; useful for this kind of example. Pretty sure gist.github.com has a raw interface too, but would have to look (whereas ix/sprunge document theirs up-front). Commented Nov 30, 2017 at 16:49
  • The fact that a call to jq can omit the filter if its standard output is a terminal appears to be undocumented. Commented Nov 30, 2017 at 18:45

1 Answer 1

10

If you want jq to format the same JSON it got as input, pass . as the script for it to run:

curl -H "Accept: application/json" https://icanhazdadjoke.com/ | jq . > file.txt

From the manual:

Identity: .

The absolute simplest filter is . . This is a filter that takes its input and produces it unchanged as output. That is, this is the identity operator.

Since jq by default pretty-prints all output, this trivial program can be a useful way of formatting JSON output from, say, curl.

Sign up to request clarification or add additional context in comments.

3 Comments

Very great!! Can you provide link to documentation or explanation of what the . does
@ktgold you've done it yourself, check the usage message in your question ;)
Please be warned that the JSON parser gets to chomp the input before .sees it, so jq . cannot always be reliably used as a pretty-printer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.