为什么用 jq returns null 解析?
why parsing with jq returns null?
我正在尝试使用 jq 工具从我的 json 文件中提取 3 个字段(状态、ID、名称)的值,这是我的 json:
猫parse.json
{
"stream": {
"_id": 65675798730520654496,
"broadcast_platform": "live",
"community_id": "",
"community_ids": [],
"average_fps": 60.0247524752,
"delay": 0,
"created_at": "2018-09-26T07:25:38Z",
"is_playlist": false,
"stream_type": "live",
"preview": {
"small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_versuta-80x4512wdfqf.jpg",
},
"channel": {
"mature": true,
"status": "status",
"broadcaster_language": "ru",
"broadcaster_software": "",
"_id": 218025408945123423423445,
"name": "djvbsdhvsdvasdv",
"created_at": "2011-04-17T17:31:36.091604Z",
"updated_at": "2018-09-26T09:49:04.434245Z",
"partner": true,
"video_banner": null,
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/25c2bec3-95b8-4347-aba0-128b3b913b0d-profile_banner-480.png",
"profile_banner_background_color": "",
"views": 103911737,
"followers": 446198,
"broadcaster_type": "",
"description": "",
"private_video": false,
"privacy_options_enabled": false
}
}
}
在线 json 验证者说它是有效的,当我尝试获取一些字段时 return null
猫parse.json | jq '.channel'
null
猫parse.json | jq'.channel.status'
null
伙计们,我做错了什么?
您的 JSON 对象有一个顶级字段 "stream" 您需要访问 "stream" 才能访问其他子属性,例如channel
:
jq '.stream.channel.status' parse.json
你也可以cat parse.json | jq '.stream.channel.status'
.
您的示例 JSON 也无效,因为 stream.preview.small
属性 有尾随逗号。不过,只需删除该逗号即可使其有效。
要处理无效的 JSON,您可以使用 JSON 整流器,例如 hjson
;为了避免与识别相关路径相关的任何麻烦,您可以使用 ..|objects
。例如:
$ hjson -j parse.json | jq '..|objects|select(.status) | .status, ._id, .name'
"status"
218025408945123440000000
"djvbsdhvsdvasdv"
我正在尝试使用 jq 工具从我的 json 文件中提取 3 个字段(状态、ID、名称)的值,这是我的 json:
猫parse.json
{
"stream": {
"_id": 65675798730520654496,
"broadcast_platform": "live",
"community_id": "",
"community_ids": [],
"average_fps": 60.0247524752,
"delay": 0,
"created_at": "2018-09-26T07:25:38Z",
"is_playlist": false,
"stream_type": "live",
"preview": {
"small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_versuta-80x4512wdfqf.jpg",
},
"channel": {
"mature": true,
"status": "status",
"broadcaster_language": "ru",
"broadcaster_software": "",
"_id": 218025408945123423423445,
"name": "djvbsdhvsdvasdv",
"created_at": "2011-04-17T17:31:36.091604Z",
"updated_at": "2018-09-26T09:49:04.434245Z",
"partner": true,
"video_banner": null,
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/25c2bec3-95b8-4347-aba0-128b3b913b0d-profile_banner-480.png",
"profile_banner_background_color": "",
"views": 103911737,
"followers": 446198,
"broadcaster_type": "",
"description": "",
"private_video": false,
"privacy_options_enabled": false
}
}
}
在线 json 验证者说它是有效的,当我尝试获取一些字段时 return null
猫parse.json | jq '.channel'
null
猫parse.json | jq'.channel.status'
null
伙计们,我做错了什么?
您的 JSON 对象有一个顶级字段 "stream" 您需要访问 "stream" 才能访问其他子属性,例如channel
:
jq '.stream.channel.status' parse.json
你也可以cat parse.json | jq '.stream.channel.status'
.
您的示例 JSON 也无效,因为 stream.preview.small
属性 有尾随逗号。不过,只需删除该逗号即可使其有效。
要处理无效的 JSON,您可以使用 JSON 整流器,例如 hjson
;为了避免与识别相关路径相关的任何麻烦,您可以使用 ..|objects
。例如:
$ hjson -j parse.json | jq '..|objects|select(.status) | .status, ._id, .name'
"status"
218025408945123440000000
"djvbsdhvsdvasdv"