• Linux| jq命令对JSON格式数据操作


    演示内容,输出到json1文件中:

    cat >  json1 << EOF
    {"tokens":[{"token":"网络","start_offset":0,"end_offset":2,"type":"CN_WORD","position":0},{"token":"不是","start_offset":2,"end_offset":4,"type":"CN_WORD","position":1},{"token":"法外","start_offset":4,"end_offset":6,"type":"CN_WORD","position":2},{"token":"之地","start_offset":6,"end_offset":8,"type":"CN_WORD","position":3}]}
    EOF
    
    • 1
    • 2
    • 3

    1、获取整个JSON对象

    cat json1 |jq .
    
    • 1

    返回内容:

    {
      "tokens": [
        {
          "token": "网络",
          "start_offset": 0,
          "end_offset": 2,
          "type": "CN_WORD",
          "position": 0
        },
        {
          "token": "不是",
          "start_offset": 2,
          "end_offset": 4,
          "type": "CN_WORD",
          "position": 1
        },
        {
          "token": "法外",
          "start_offset": 4,
          "end_offset": 6,
          "type": "CN_WORD",
          "position": 2
        },
        {
          "token": "之地",
          "start_offset": 6,
          "end_offset": 8,
          "type": "CN_WORD",
          "position": 3
        }
      ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    2、获取所有数组

    cat json1 |jq '.[]'
    
    • 1

    返回内容:

    [
      {
        "token": "网络",
        "start_offset": 0,
        "end_offset": 2,
        "type": "CN_WORD",
        "position": 0
      },
      {
        "token": "不是",
        "start_offset": 2,
        "end_offset": 4,
        "type": "CN_WORD",
        "position": 1
      },
      {
        "token": "法外",
        "start_offset": 4,
        "end_offset": 6,
        "type": "CN_WORD",
        "position": 2
      },
      {
        "token": "之地",
        "start_offset": 6,
        "end_offset": 8,
        "type": "CN_WORD",
        "position": 3
      }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3、获取tokens数组中第1个索引数据

    cat json1 |jq '.tokens[0]'
    
    • 1

    返回内容:

    {
      "token": "网络",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、获取tokens数组中第1、2个索引数据

    cat json1 |jq '.tokens[0:2]'
    
    • 1

    返回内容:

    [
      {
        "token": "网络",
        "start_offset": 0,
        "end_offset": 2,
        "type": "CN_WORD",
        "position": 0
      },
      {
        "token": "不是",
        "start_offset": 2,
        "end_offset": 4,
        "type": "CN_WORD",
        "position": 1
      }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5、获取tokens数组中第1个索引数据中token的值

    cat json1 |jq '.tokens[0].token'
    
    • 1

    返回内容:

    "网络"
    
    • 1

    6、获取tokens数组中第1个索引数据中tokentype的值

    cat json1 |jq '.tokens[0].token,.tokens[0].type'
    
    • 1

    可以简写成:

    cat json1 |jq '.tokens[0]|.token,.type'
    
    • 1

    返回内容:

    "网络"
    "CN_WORD"
    
    • 1
    • 2

    7、获取tokens数组中所有token的值

    cat json1 |jq '.tokens[].token'
    
    • 1

    返回内容:

    "网络"
    "不是"
    "法外"
    "之地"
    
    • 1
    • 2
    • 3
    • 4

    8、迭代数组,使用map函数对数组中的每个元素执行相同的操作

    cat json1 |jq '.[]|map(.token)'
    
    • 1

    返回内容:

    [
      "网络",
      "不是",
      "法外",
      "之地"
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    9、添加过滤条件:获取tokens数组中token == “网络” 的数据

    cat json1 |jq '.tokens[]|select(.token == "网络")'
    
    • 1

    返回内容:

    {
      "token": "网络",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    10、添加多个过滤条件:获取tokens数组中token == "网络" and type == "CN_WORD" 的数据

    cat json1 |jq '.tokens[]|select(.token == "网络" and .type == "CN_WORD")'
    
    • 1

    返回内容:

    {
      "token": "网络",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    11、添加过滤条件:获取tokens数组中token == "网络" or token == "法外" 的数据

    cat json1 |jq '.tokens[]|select(.token == "网络" or .token == "法外")'
    
    • 1

    返回内容:

    {
      "token": "网络",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    }
    {
      "token": "法外",
      "start_offset": 4,
      "end_offset": 6,
      "type": "CN_WORD",
      "position": 2
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    12、获取所有数组长度 length函数

    cat json1 |jq '.[]|length'
    
    • 1

    返回内容:

    4
    
    • 1

    13、修改tokens数组中所有token值改为TEST

    cat json1 |jq '.tokens|map(.token="TEST")'
    
    • 1

    返回内容:

    [
      {
        "token": "TEST",
        "start_offset": 0,
        "end_offset": 2,
        "type": "CN_WORD",
        "position": 0
      },
      {
        "token": "TEST",
        "start_offset": 2,
        "end_offset": 4,
        "type": "CN_WORD",
        "position": 1
      },
      {
        "token": "TEST",
        "start_offset": 4,
        "end_offset": 6,
        "type": "CN_WORD",
        "position": 2
      },
      {
        "token": "TEST",
        "start_offset": 6,
        "end_offset": 8,
        "type": "CN_WORD",
        "position": 3
      }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    14、转换成字符串tostring函数

    cat json1 | jq '.tokens|tostring'
    
    • 1

    返回内容:

    "[{\"token\":\"网络\",\"start_offset\":0,\"end_offset\":2,\"type\":\"CN_WORD\",\"position\":0},{\"token\":\"不是\",\"start_offset\":2,\"end_offset\":4,\"type\":\"CN_WORD\",\"position\":1},{\"token\":\"法外\",\"start_offset\":4,\"end_offset\":6,\"type\":\"CN_WORD\",\"position\":2},{\"token\":\"之地\",\"start_offset\":6,\"end_offset\":8,\"type\":\"CN_WORD\",\"position\":3}]"
    
    • 1

    15、if-then-else条件判断语句
    判断token值是否=网络,如果满足输出value = 网络,不满足输出value != 网络

    cat json1 |jq '.tokens[]|if .token== "网络" then "value = 网络" else "value != 网络" end'
    
    • 1

    返回内容

    "value = 网络"
    "value != 网络"
    "value != 网络"
    "value != 网络"
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    UniApp 踩坑日记
    Linux实验五:进程管理
    Netty核心源码剖析(四)
    java基础10题
    【漏洞复现】用友GPR-U8 slbmbygr SQL注入漏洞
    【数据治理】数据治理标准化白皮书 (2021 年)
    C语言——数组详解
    kkFileView getCorsFile 任意文件读取漏洞(CVE-2021-43734)
    戴尔服务器安装Debian11过程
    匿名类为什么不可以使用非final变量
  • 原文地址:https://blog.csdn.net/weixin_45310323/article/details/133221538