Demonstration content, output to json1 file:
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. Get the entire JSON object
cat json1 |jq .
Return content:
{
"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. Get all arrays
cat json1 |jq '.[]'
Return content:
[
{
"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. Get tokens
the first index data in the array
cat json1 |jq '.tokens[0]'
Return content:
{
"token": "网络",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}
4. Get tokens
the 1st and 2nd index data in the array
cat json1 |jq '.tokens[0:2]'
Return content:
[
{
"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. Get the value tokens
in the first index data in the arraytoken
cat json1 |jq '.tokens[0].token'
Return content:
"网络"
6. Get the value of in tokens
the first index data in the arraytoken
type
cat json1 |jq '.tokens[0].token,.tokens[0].type'
It can be abbreviated as:
cat json1 |jq '.tokens[0]|.token,.type'
Return content:
"网络"
"CN_WORD"
7. Get all the values tokens
in the arraytoken
cat json1 |jq '.tokens[].token'
Return content:
"网络"
"不是"
"法外"
"之地"
8. Iterate over the array and use map
functions to perform the same operation on each element in the array.
cat json1 |jq '.[]|map(.token)'
Return content:
[
"网络",
"不是",
"法外",
"之地"
]
9. Add filter conditions: Get tokens
the data of token == "network" in the array
cat json1 |jq '.tokens[]|select(.token == "网络")'
Return content:
{
"token": "网络",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}
10. Add multiple filter conditions: get the data of and tokens
in the arraytoken == "网络"
type == "CN_WORD"
cat json1 |jq '.tokens[]|select(.token == "网络" and .type == "CN_WORD")'
Return content:
{
"token": "网络",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
}
11. Add filter conditions: get the data of or tokens
in the arraytoken == "网络"
token == "法外"
cat json1 |jq '.tokens[]|select(.token == "网络" or .token == "法外")'
Return content:
{
"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. Get the length of all arrayslength函数
cat json1 |jq '.[]|length'
Return content:
4
13. Modify tokens
all token values in the array to TEST
cat json1 |jq '.tokens|map(.token="TEST")'
Return content:
[
{
"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. Convert to string,tostring函数
cat json1 | jq '.tokens|tostring'
Return content:
"[{
\"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
Conditional judgment statement
: Determine token
whether the value = network, if it meets the output value = 网络
, if it does not, the outputvalue != 网络
cat json1 |jq '.tokens[]|if .token== "网络" then "value = 网络" else "value != 网络" end'
Return content
"value = 网络"
"value != 网络"
"value != 网络"
"value != 网络"