• 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
  • 相关阅读:
    俄罗斯方块游戏开发教程7:消除判断和处理
    WPF 常用功能整合
    RISC-V 编译环境搭建:riscv-gnu-toolchain 和 riscv-tools
    前端的那些必须要掌握的样式设置和布局【CSS、浮动、清除、固定、em、rem等等......】
    力扣热100 滑动窗口
    UVa11887 Tetrahedrons and Spheres
    Xposed hook SensorManager
    【C++】面向对象编程(三)定义一个“抽象基类”的三大步骤
    只因说了一句:“反正我有技术,在哪不一样”一位年薪35W自动化测试工程师被某讯开除
    HOSMEL:一种面向中文的可热插拔模块化实体链接工具包
  • 原文地址:https://blog.csdn.net/weixin_45310323/article/details/133221538