jq 解析json数据

jq 安装方法: Use Homebrew to install jq 1.5 withbrew install jq
jq 解析son数据,直接示例

$ cat 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"
                        }
                ]
}

To parse a JSON object:

$ cat json.txt | jq '.location.city'

"Mountain View"

注意

jq能处理的需要是严格的JSON格式数据,JSON对象和JSON字符串是不行的,如下面的两种格式数据jq是不能处理的
注意点引文 ,看官方文档没看不懂,继续努力

jq解析json字符串/shell中字符串转换为数据

(偶然发现,管道的作用这么强大)
利用管道,示例代码如下:

"message": "",
"data": {
"appKey": "cxxxxxxxx",
"userKey": "xxxxxxx",
"appType": "1",
"appIsLastest": "1",
"appFileSize": "73426",
"appName": "CoreGraphicsTest",
"appVersion": "1.0",
"appVersionNo": "1",
"appBuildVersion": "1",
"appIdentifier": "com.www.CoreGraphicsTest",
"appIcon": "",
"appDescription": "",
"appUpdateDescription": "",
"appScreenshots": "",
"appShortcutUrl": "XTttt",
"appCreated": "2016-07-26 15:27:56",
"appUpdated": "2016-07-26 15:27:56",
"appQRCodeURL": "https:example.com"
}
}'

echo $jsonstest | jq '.data'

你可能感兴趣的:(jq 解析json数据)