【问题标题】:json jq request formatjson jq请求格式
【发布时间】:2021-12-05 20:23:16
【问题描述】:

我需要帮助才能使用 jq 从这个 JSON 文件中提取数据。

应用程序flv 然后验证流名称mystrame 是否处于活动状态并提取meta.video.height

我确实尝试了很多查询但没有成功,而且我的 jq 知识很差。

{
  "port": 1935,
  "server_index": 0,
  "applications": [{
      "name": "hls",
      "live": {
        "streams": [{
          "name": "donbosco",
          "time": 2380739,
          "bw_in": 2112440,
          "bytes_in": 541618713,
          "bw_out": 0,
          "bytes_out": 0,
          "bw_audio": 35544,
          "bw_video": 2076888,
          "clients": [{
            "id": 453,
            "address": "127.0.0.1",
            "time": 2380959,
            "flashver": "FMLE/3.0 (compatible; Lavf57.83.100)",
            "dropped": 0,
            "avsync": 28,
            "timestamp": 2382635,
            "publishing": true,
            "active": true
          }],
          "records": [],
          "meta": {
            "video": {
              "width": 1168,
              "height": 720,
              "frame_rate": 25,
              "codec": "H264",
              "profile": "High",
              "level": 3.1
            },
            "audio": {
              "codec": "AAC",
              "profile": "LC",
              "channels": 2,
              "sample_rate": 16000
            }
          },
          "nclients": 1,
          "publishing": true,
          "active": true
        }],
        "nclients": 1
      },
      "recorders": {
        "count": 0,
        "lists": []
      }
    },
    {
      "name": "flv",
      "live": {
        "streams": [{
          "name": "mystream",
          "time": 2382811,
          "bw_in": 2059096,
          "bytes_in": 541841549,
          "bw_out": 2059096,
          "bytes_out": 543351459,
          "bw_audio": 35472,
          "bw_video": 2023624,
          "clients": [{
              "id": 452,
              "address": "127.0.0.1",
              "time": 2382727,
              "flashver": "LNX 9,0,124,2",
              "dropped": 0,
              "avsync": -12,
              "timestamp": 2384520,
              "publishing": false,
              "active": true
            },
            {
              "id": 451,
              "address": "127.0.0.1",
              "time": 2383031,
              "flashver": "FMLE/3.0 (compatible; Lavf58.74.100)",
              "dropped": 0,
              "avsync": -12,
              "timestamp": 2384520,
              "publishing": true,
              "active": true
            }
          ],
          "records": [],
          "meta": {
            "video": {
              "width": 1168,
              "height": 720,
              "frame_rate": 25,
              "codec": "H264",
              "profile": "High",
              "level": 3.1
            },
            "audio": {
              "codec": "AAC",
              "profile": "LC",
              "channels": 2,
              "sample_rate": 16000
            }
          },
          "nclients": 2,
          "publishing": true,
          "active": true
        }],
        "nclients": 2
      },
      "recorders": {
        "count": 0,
        "lists": []
      }
    }
  ]
}

【问题讨论】:

    标签: json jq


    【解决方案1】:

    您是在寻求有关命令行 JSON 处理器 jq 或 JavaScript 库 jQuery 的帮助吗?它们是不相同的。对于jq,试试

    jq '
      .applications
      | map(select(.name == "flv"))[].live.streams
      | map(select(.name == "mystream" and .active))[].meta.video.height
    '
    
    720
    

    Demo

    【讨论】:

    • 我相信他是在寻求 jq 的帮助。我错误地编辑了问题,将 jq 更改为给定标签的 jQuery,但是我错过了 jq tg。我已将问题中的语言恢复为原来的样子。 OP 需要更清楚地标记问题,并表现出一些努力。
    • 就是这样!是的,这是我想使用的 jq。非常感谢pmf,是的,很抱歉问我这样的问题....它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多