6

If i curl to some site I can get straight json:

curl http://httpbin.org/ip

{ "origin": "37.77.126.22"}

to beautify, i do:

curl http://httpbin.org/ip | jq

{
   "origin": "37.77.126.22"
}

to beautify it and save it, I redirect...but it does not work

curl http://httpbin.org/ip | jq > output.txt

{
   "origin": "37.77.126.22"
}

(23) Failed writing body

How should it be done?

  • I got already beautified json without jq when I run your command – RomanPerekhrest May 25 '17 at 8:06
  • This worked for me – cardamom May 15 at 7:46
6

I'm a little surprised that you get the JSON output in the second example. On my system, jq outputs an error message that also contains usage information:

$ curl "http://httpbin.org/ip" | jq >file
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    33  100    33    0     0    115      0 --:--:-- --:--:-- --:--:--   136
(23) Failed writing body

The "Failed writing body" comes from curl as jq has exited and is not available to read the body (contents of the web page).


jq needs a filter expression. The simplest filter is . (dot) which acts like a "pass-through" filter (what's called "identity filter" in the usage info above):

$ curl "http://httpbin.org/ip" | jq . >file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.