echo json.txt | jq '.' 输出整个json字符串
echo json.txt | jq '.[0]' 取出数组中第一个object
echo json.txt | jq '.[0].name' 取出数组第一个object中键为name的值
root@JDBox_LuBan:~# cat json.txt | jq '.[0].name' "Google"
json.txt:
[ { "name": "Google", "location": { "street": "1600 Amphitheatre Parkway", "city": "Mountain View", "state": "California", "country": "US" }, "employees": [ { "name": "Michael", "division": "Engineering" }, { "name": "Laura", "division": "HR" }, { "name": "Elise", "division": "Marketing" } ] } ]
1.嵌套解析:
# cat json.txt | jq '.[0].location.city' "Mountain View"
2.获取JSON中的所有键:
# cat json.txt | jq '.[0]|keys' [ "employees", "location", "name" ]
3.获取键值对的个数,或者数组长度:
cat json.txt | jq '.[0].employees|length' 3
# 取键foo的值: echo '{"foo": 42, "bar": "less interesting data"}' | jq '.foo' 42 # 获取json所有键: echo '{"foo": 42, "bar": "less interesting data"}' | jq 'keys' [ "bar", "foo" ] # has判断是否存在某个key echo '{"foo": 42, "bar": "less interesting data"}' | jq 'has("foo")' true # 获取键值对的个数,或者数组长度 echo '{"foo": 42, "bar": "less interesting data"}' | jq 'length' 2
参考:https://blog.csdn.net/yanbingquan/article/details/50770911
标签:shell,name,jq,echo,json,foo,txt From: https://www.cnblogs.com/v5captain/p/16932237.html