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、获取整个JSON对象

cat json1 |jq .

返回内容:

{
  "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
    }
  ]
}

2、获取所有数组

cat json1 |jq '.[]'

返回内容:

[
  {
    "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
  }
]

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

cat json1 |jq '.tokens[0]'

返回内容:

{
  "token": "网络",
  "start_offset": 0,
  "end_offset": 2,
  "type": "CN_WORD",
  "position": 0
}

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

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

返回内容:

[
  {
    "token": "网络",
    "start_offset": 0,
    "end_offset": 2,
    "type": "CN_WORD",
    "position": 0
  },
  {
    "token": "不是",
    "start_offset": 2,
    "end_offset": 4,
    "type": "CN_WORD",
    "position": 1
  }
]

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

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

返回内容:

"网络"

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

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

可以简写成:

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

返回内容:

"网络"
"CN_WORD"

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

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

返回内容:

"网络"
"不是"
"法外"
"之地"

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

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

返回内容:

[
  "网络",
  "不是",
  "法外",
  "之地"
]

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

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

返回内容:

{
  "token": "网络",
  "start_offset": 0,
  "end_offset": 2,
  "type": "CN_WORD",
  "position": 0
}

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

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

返回内容:

{
  "token": "网络",
  "start_offset": 0,
  "end_offset": 2,
  "type": "CN_WORD",
  "position": 0
}

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

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

返回内容:

{
  "token": "网络",
  "start_offset": 0,
  "end_offset": 2,
  "type": "CN_WORD",
  "position": 0
}
{
  "token": "法外",
  "start_offset": 4,
  "end_offset": 6,
  "type": "CN_WORD",
  "position": 2
}

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

cat json1 |jq '.[]|length'

返回内容:

4

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

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

返回内容:

[
  {
    "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
  }
]

14、转换成字符串,tostring函数

cat json1 | jq '.tokens|tostring'

返回内容:

"[{\"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}]"

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

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

返回内容

"value = 网络"
"value != 网络"
"value != 网络"
"value != 网络"

你可能感兴趣的:(Linux运维,Shell,linux,json,c#,shell)