首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQ如果否则返回空

JQ如果否则返回空
EN

Stack Overflow用户
提问于 2022-08-31 09:26:07
回答 1查看 161关注 0票数 -3

我试图用jq过滤和输出JSON。

API有时会返回一个对象,有时返回一个数组,我希望使用if语句捕捉结果,并在对象/数组不可用时返回空字符串。

代码语言:javascript
复制
{
    "result": 
        {
            "entry": {
                "id": "207579",
                "title": "Realtek Bluetooth Mesh SDK on Linux\/Android Segmented Packet reference buffer overflow",
                "summary": "A vulnerability, which was classified as critical, was found in Realtek Bluetooth Mesh SDK on Linux\/Android (the affected version unknown). This affects an unknown functionality of the component Segmented Packet Handler. There is no information about possible countermeasures known. It may be suggested to replace the affected object with an alternative product.",
                "details": {
                    "affected": "A vulnerability, which was classified as critical, was found in Realtek Bluetooth Mesh SDK on Linux\/Android (the affected version unknown).",
                    "vulnerability": "The manipulation of the argument reference with an unknown input leads to a unknown weakness. CWE is classifying the issue as CWE-120. The program copies an input buffer to an output buffer without verifying that the size of the input buffer is less than the size of the output buffer, leading to a buffer overflow.",
                    "impact": "This is going to have an impact on confidentiality, integrity, and availability.",
                    "countermeasure": "There is no information about possible countermeasures known. It may be suggested to replace the affected object with an alternative product."
                },
                "timestamp": {
                    "create": "1661860801",
                    "change": "1661861110"
                },
                "changelog": [
                    "software_argument"
                ]
            },
            "software": {
                "vendor": "Realtek",
                "name": "Bluetooth Mesh SDK",
                "platform": [
                    "Linux",
                    "Android"
                ],
                "component": "Segmented Packet Handler",
                "argument": "reference",
                "cpe": [
                    "cpe:\/a:realtek:bluetooth_mesh_sdk"
                ],
                "cpe23": [
                    "cpe:2.3:a:realtek:bluetooth_mesh_sdk:*:*:*:*:*:*:*:*"
                ]
            }
        }
    
}

还希望对整个数组输出全局使用该语句,以便我可以将其解析为.csv并转义null,因为软件名称也可以包含数组或对象。具有全局if语句,简化语法结果,并使用?抑制错误

我从bash收到的错误

代码语言:javascript
复制
jq  -r '.result []   |  [ "https://vuldb.com/?id." + .entry.id ,.software.vendor // "empty",(.software.name | if type!="array" then [.] | join (",")  else . //"empty" end  )?,.software.type //  "empty",(.software.platform | if type!="array" then []  else . | join (",") //"empty" end )?] | @csv' > tst.csv
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7452    0  7393  100    59   4892     39  0:00:01  0:00:01 --:--:--  4935
jq: error (at <stdin>:182): Cannot iterate over null (null)

我尝试的是下面的代码,我试图演示https://jqplay.org/,这是不正确的语法

代码语言:javascript
复制
.result [] |( if .[] == null  then // "empty" else . end 
| ,software.name // "empty" ,.software.platform |if type!="array" then [.]  // "empty" else . | join (",")  end)

电流输出

代码语言:javascript
复制
[
  [
    "Bluetooth Mesh SDK"
  ],
  "Linux,Android"
]

期望结果

代码语言:javascript
复制
[
  
  "Bluetooth Mesh SDK",
  "empty"
]
EN

回答 1

Stack Overflow用户

发布于 2022-08-31 12:59:51

在修复输入JSON之后,我认为您可以使用以下JQ过滤器获得所需的输出:

代码语言:javascript
复制
if (.result | type) == "array" then . else (.result |= [.]) end \
    | .result[].software | [ .name, (.platform // [ "Empty" ] | join(",")) ]

哪里

  • if (.result | type) == "array" then . else (.result |= [.]) end 如果.result不是数组,则将type封装在数组中
  • .result[].software 循环通过每个software obj中的.result
  • [ .name, (.platform // [ "Empty" ] | join(",")) ] 使用.name.platform创建一个数组(当它不存在时由[ "Empty" ]替换。然后是字符串的join()d

结果:

代码语言:javascript
复制
[
  "Bluetooth Mesh SDK",
  "Linux,Android"
]

在线演示

代码语言:javascript
复制
[
  "Bluetooth Mesh SDK",
  "Empty
]

在线演示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73553724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档