我试图用jq过滤和输出JSON。
API有时会返回一个对象,有时返回一个数组,我希望使用if语句捕捉结果,并在对象/数组不可用时返回空字符串。
{
"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收到的错误
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/,这是不正确的语法
.result [] |( if .[] == null then // "empty" else . end
| ,software.name // "empty" ,.software.platform |if type!="array" then [.] // "empty" else . | join (",") end)电流输出
[
[
"Bluetooth Mesh SDK"
],
"Linux,Android"
]期望结果
[
"Bluetooth Mesh SDK",
"empty"
]发布于 2022-08-31 12:59:51
在修复输入JSON之后,我认为您可以使用以下JQ过滤器获得所需的输出:
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结果:
[
"Bluetooth Mesh SDK",
"Linux,Android"
]或
[
"Bluetooth Mesh SDK",
"Empty
]https://stackoverflow.com/questions/73553724
复制相似问题