combinar tubería y redirigir en curl y jq

combinar tubería y redirigir en curl y jq

Si visito algún sitio, puedo obtener json directo:

curl http://httpbin.org/ip

{ "origin": "37.77.126.22"}

para embellecer, hago:

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

{
   "origin": "37.77.126.22"
}

para embellecerlo y guardarlo lo redirijo...pero no funciona

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

{
   "origin": "37.77.126.22"
}

(23) Failed writing body

¿Cómo deberia hacerse?

Respuesta1

Me sorprende que obtenga el resultado JSON en el último ejemplo, pero puede ser un error de cortar y pegar en la pregunta.

En mi sistema, jqaparece un mensaje de error que también contiene información de uso:

$ 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

El "Cuerpo de escritura fallido" proviene de curlque jqsalió debido al error y no está disponible para leer el cuerpo (contenido de la página web).


Al escribir en cualquier otra cosa que no sea una terminal, jq1.5 necesita una expresión de filtro. El filtro más simple es .(punto), que actúa como un filtro de "paso" (lo que se llama "filtro de identidad" en la información de uso anterior):

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

Las versiones posteriores jqutilizan el filtro de identidad de forma predeterminada, incluso cuando se escribe en un archivo o canalización, en el caso de que no se proporcione ningún filtro explícitamente.

información relacionada