在jq

时间:2019-07-19 10:35:16

标签: json jq

我正在尝试从json中的键获取多个值。我已经完成了第一部分,但第二部分没有给我我想要的东西。

这是视频json中一些数据的摘要:

{
"streams": [
    {
        "index": 0,
        "codec_name": "h264",
        "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
        "profile": "Main",
        "codec_type": "video",
        "codec_time_base": "0/2",
        "codec_tag_string": "[27][0][0][0]",
        "codec_tag": "0x001b",
        "width": 1920,
        "height": 1080,
        "coded_width": 1920,
        "coded_height": 1088,
        "has_b_frames": 1,
        "sample_aspect_ratio": "1:1",
        "display_aspect_ratio": "16:9",
        "pix_fmt": "yuv420p",
        "level": 40,
        "chroma_location": "left",
        "field_order": "progressive",
        "refs": 1,
        "is_avc": "false",
        "nal_length_size": "0",
        "id": "0x100",
        "r_frame_rate": "25/1",
        "avg_frame_rate": "0/0",
        "time_base": "1/90000",
        "start_pts": 1397779146,
        "start_time": "15530.879400",
        "bits_per_raw_sample": "8",
        "disposition": {
            "default": 0,
            "dub": 0,
            "original": 0,
            "comment": 0,
            "lyrics": 0,
            "karaoke": 0,
            "forced": 0,
            "hearing_impaired": 0,
            "visual_impaired": 0,
            "clean_effects": 0,
            "attached_pic": 0,
            "timed_thumbnails": 0
        }
    },
    {
        "index": 1,
        "codec_name": "aac",
        "codec_long_name": "AAC (Advanced Audio Coding)",
        "codec_type": "audio",
        "codec_time_base": "1/0",
        "codec_tag_string": "[15][0][0][0]",
        "codec_tag": "0x000f",
        "sample_fmt": "fltp",
        "sample_rate": "0",
        "channels": 0,
        "bits_per_sample": 0,
        "id": "0x101",
        "r_frame_rate": "0/0",
        "avg_frame_rate": "0/0",
        "time_base": "1/90000",
        "disposition": {
            "default": 0,
            "dub": 0,
            "original": 0,
            "comment": 0,
            "lyrics": 0,
            "karaoke": 0,
            "forced": 0,
            "hearing_impaired": 0,
            "visual_impaired": 0,
            "clean_effects": 0,
            "attached_pic": 0,
            "timed_thumbnails": 0
        },
        "tags": {
            "language": "eng"
        }
    },
    {
        "index": 2,
        "codec_name": "dvb_teletext",
        "codec_long_name": "DVB teletext",
        "codec_type": "subtitle",
        "codec_tag_string": "[6][0][0][0]",
        "codec_tag": "0x0006",
        "id": "0x102",
        "r_frame_rate": "0/0",
        "avg_frame_rate": "0/0",
        "time_base": "1/90000",
        "disposition": {
            "default": 0,
            "dub": 0,
            "original": 0,
            "comment": 0,
            "lyrics": 0,
            "karaoke": 0,
            "forced": 0,
            "hearing_impaired": 0,
            "visual_impaired": 0,
            "clean_effects": 0,
            "attached_pic": 0,
            "timed_thumbnails": 0
        },
        "tags": {
            "language": "eng"
        }
    }
] 
}

所以我想先获取视频信息,然后获取音频信息:

预期结果:

"1080,h264,aac"

这是我正在尝试的代码,这给了我空白的输出:

.streams[] | "\(select(.codec_type=="video") | "\(.height),\(.codec_name)"),\(select(.codec_type=="audio") | "\(.codec_name)")"

但是,当引用单个数组时,它将起作用:

.streams[] | "\(select(.codec_type=="video") | "\(.height),\(.codec_name)"),"
"1080,h264,"

我想要音频和视频信息。

Jq Play

2 个答案:

答案 0 :(得分:0)

以下似乎是您想要的:

.streams
| (.[] | select(.codec_type == "video") | "\(.height),\(.codec_name),")
+ (.[] | select(.codec_type == "audio") | "\(.codec_name)") 

它确实会产生所需的输出,并且确实与问题描述和示例代码相匹配,但是(例如)如果有多个视频对象,或者没有音频对象,则结果可能不完全是您想要的。

答案 1 :(得分:0)

作为替代,可以使用基于步行路径的Unix实用程序 jtc 选择所需的记录:

bash $ <video.json jtc -w'[codec_type]:<video>[-1][height]<H>v[-1][codec_name]<C>v[-2][codec_type]:<audio>[-1][codec_name]' -T'"{H},{C},{}"'
"1080,h264,aac"
bash $ 

PS>披露:我是jtc-用于JSON操作的shell cli工具的创建者