shell脚本吧 关注:330贴子:701
  • 4回复贴,共1

记录一下,jq工具的使用

只看楼主收藏回复

rt


IP属地:上海1楼2021-01-31 16:09回复
    jq最简单的功能就是格式化。
    给一段json(下面一直要用到):
    {"code":0,"data":{"01":[{"answer":{"time_consume":"0.32","records":[{"ttl":5,"value":"39.156.69.79","ip_location":"北京"},{"ttl":5,"value":"220.181.38.148","ip_location":"北京"}],"error":""}}],"852":[{"answer":{"time_consume":"0.23","records":[{"ttl":600,"value":"39.156.69.79","ip_location":"北京"},{"ttl":600,"value":"220.181.38.148","ip_location":"北京"}],"error":""}}],"86":[{"answer":{"time_consume":"0.00","records":[{"ttl":4,"value":"220.181.38.148","ip_location":"北京"},{"ttl":4,"value":"39.156.69.79","ip_location":"北京"}],"error":""}}]}}
    看着心累,对不对?


    IP属地:上海2楼2021-01-31 21:18
    回复
      这时,我们可以:
      $ echo '{"code":0,"data":{"01":[{"answer":{"time_consume":"0.32","records":[{"ttl":5,"value":"39.156.69.79","ip_location":"北京"},{"ttl":5,"value":"220.181.38.148","ip_location":"北京"}],"error":""}}],"852":[{"answer":{"time_consume":"0.23","records":[{"ttl":600,"value":"39.156.69.79","ip_location":"北京"},{"ttl":600,"value":"220.181.38.148","ip_location":"北京"}],"error":""}}],"86":[{"answer":{"time_consume":"0.00","records":[{"ttl":4,"value":"220.181.38.148","ip_location":"北京"},{"ttl":4,"value":"39.156.69.79","ip_location":"北京"}],"error":""}}]}}' | jq
      它会把这一长串json格式化为人看的东西


      IP属地:上海3楼2021-01-31 21:20
      回复
        就是这样的(排版被吃了):
        {
        "code": 0,
        "data": {
        "01": [
        {
        "answer": {
        "time_consume": "0.32",
        "records": [
        {
        "ttl": 5,
        "value": "39.156.69.79",
        "ip_location": "北京"
        },
        {
        "ttl": 5,
        "value": "220.181.38.148",
        "ip_location": "北京"
        }
        ],
        "error": ""
        }
        }
        ],
        "852": [
        {
        "answer": {
        "time_consume": "0.23",
        "records": [
        {
        "ttl": 600,
        "value": "39.156.69.79",
        "ip_location": "北京"
        },
        {
        "ttl": 600,
        "value": "220.181.38.148",
        "ip_location": "北京"
        }
        ],
        "error": ""
        }
        }
        ],
        "86": [
        {
        "answer": {
        "time_consume": "0.00",
        "records": [
        {
        "ttl": 4,
        "value": "220.181.38.148",
        "ip_location": "北京"
        },
        {
        "ttl": 4,
        "value": "39.156.69.79",
        "ip_location": "北京"
        }
        ],
        "error": ""
        }
        }
        ]
        }
        }


        IP属地:上海4楼2021-01-31 21:20
        回复
          并且,还是带高亮的!
          这只是最基础的用法,jq还有很多高级用法,比如筛选数据。


          IP属地:上海5楼2021-01-31 21:22
          回复