Linux 通過命令列解析JSON字串

weixin_34205076發表於2017-11-08

1、JSON命令:

1
2
3
4
$ wget http://stedolan.github.io/jq/download/linux32/jq (32-bit system)
$ wget http://stedolan.github.io/jq/download/linux64/jq (64-bit system)
chmod +x ./jq
cp jq /usr/bin

2、JSON Schema:

json.txt

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
{
        "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"
                        }
                ]
}
3、解析JSON object:
1
2
cat json.txt | jq '.name'
"Google"

4、解析巢狀的JSON物件: 
1
2
cat json.txt | jq '.location.city'
"Mountain View"

5、解析JSON陣列:
1
2
3
4
5
cat json.txt | jq '.location | {street, city}'
{
  "city""Mountain View",
  "street""1600 Amphitheatre Parkway"
}









本文轉自 Art_Hero 51CTO部落格,原文連結:http://blog.51cto.com/curran/1353409,如需轉載請自行聯絡原作者

相關文章