How to use `jq` in a shell pipeline?

前端 未结 2 1492
执念已碎
执念已碎 2020-12-22 17:42

I can\'t seem to get jq to behave \"normally\" in a shell pipeline. For example:

$ curl -s https://api.github.com/users/octocat/repos | jq | ca         


        
相关标签:
2条回答
  • 2020-12-22 18:11

    You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing jq provides by default, use the identity filter .:

    curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
    
    0 讨论(0)
  • 2020-12-22 18:33

    One use case I have found myself doing frequently as well is "How do I construct JSON data to supply into other shell commands, for example curl?" The way I do this is by using the --null-input/-n option:

    Don’t read any input at all! Instead, the filter is run once using null as the input. This is useful when using jq as a simple calculator or to construct JSON data from scratch.

    And an example passing it into curl:

    jq -n '{key: "value"}' | curl -d @- \
      --url 'https://some.url.com' \
      -H 'Content-Type: application/json' \
      -H 'Accept: application/json'
    
    0 讨论(0)
提交回复
热议问题