html tool

2018年7月17日星期二

jq指定json的key值显示


https://stedolan.github.io/jq/tutorial/

[popexizhi:
10 cat ${api_name}/v3_j_${domain}.txt_temp|../jq -S '[.history_ips[]|{date: .date,ips: .ips}]' >${api_name}/v3_j_${domain}.txt
 11 cat ${api_name}/v3_j_${domain}.txt_temp|../jq -S '{resource:.resource,response_code:.response_code}' >>${api_name}/v3_j_${domain}.txt

]

GitHub has a JSON API, so let’s play with that. This URL gets us the last 5 commits from the jq repo.
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'
Show result
[
  {
    "sha": "d25341478381063d1c76e81b3a52e0592a7c997f",
    "commit": {
      "author": {
        "name": "Stephen Dolan",
        "email": "mu@netsoc.tcd.ie",
        "date": "2013-06-22T16:30:59Z"
      },
      "committer": {
        "name": "Stephen Dolan",
        "email": "mu@netsoc.tcd.ie",
        "date": "2013-06-22T16:30:59Z"
      },
      "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
      "tree": {
        "sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40",
        "url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40"
      },
      "url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
      "comment_count": 0
    },
    "url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f",
    "html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f",
    "comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments",
    "author": {
      "login": "stedolan",
...
There’s a lot of info we don’t care about there, so we’ll restrict it down to the most interesting fields.
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
Show result
{
  "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
  "name": "Stephen Dolan"
}
The | operator in jq feeds the output of one filter (.[0] which gets the first element of the array in the response) into the input of another ({...}which builds an object out of those fields). You can access nested attributes, such as .commit.message.
Now let’s get the rest of the commits.
jq '.[] | {message: .commit.message, name: .commit.committer.name}'
Show result
{
  "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
  "name": "Stephen Dolan"
}
{
  "message": "Reject all overlong UTF8 sequences.",
  "name": "Stephen Dolan"
}
{
  "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences.",
  "name": "Stephen Dolan"
}
{
  "message": "Fix example in manual for `floor`. See #155.",
  "name": "Stephen Dolan"
}
{
  "message": "Document floor",
  "name": "Nicolas Williams"
}
.[] returns each element of the array returned in the response, one at a time, which are all fed into {message: .commit.message, name: .commit.committer.name}.

没有评论:

发表评论