英文:
JQ Sorting by value
问题
我正在尝试使用JQ按自定义值映射对数组进行排序,并且难以弄清楚如何做到这一点。希望这里有人可以指出一种方法来实现这个目标。
我有一个包含软件漏洞描述的对象数组,并希望按严重性值的顺序对这些对象进行排序:CRITICAL、HIGH、MEDIUM、LOW
数组看起来像这样:
{
"vulnerabilities": [
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "MEDIUM",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "HIGH",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "CRITICAL",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "LOW",
"Version": "6.3-2",
"Description": "example"
}
]
}
是否有一种相对简单的方法可以使用JQ来实现这个?
谢谢!
我已经尝试使用JQ中的sort_by()函数,但似乎无法弄清楚如何应用自定义顺序(不是按字母顺序排列)。
英文:
I am looking to sort an array in JQ by a custom map of values and struggling to figure it out. Hoping someone here can point out a way to do this.
I have an array of objects that describe software vulnerabilities and want to sort the objects in order by Severity value: CRITICAL, HIGH, MEDIUM, LOW
The array looks like this:
{
"vulnerabilities": [
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "MEDIUM",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "HIGH",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "CRITICAL",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "LOW",
"Version": "6.3-2",
"Description": "example"
}
]
}
Is there an easy-ish way to do this with JQ?
Thank you!
I have tried using the sort_by() function in JQ but can't seem to figure out how to apply a custom order (not looking to organize alphabetically)
答案1
得分: 3
您可以创建一个布尔值流,并按照它进行排序。 (使用!=
表示相同顺序,==
表示相反顺序。)
.vulnerabilities |= sort_by(.Severity != ("CRITICAL","HIGH","MEDIUM","LOW"))
{
"vulnerabilities": [
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "CRITICAL",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "HIGH",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "MEDIUM",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "LOW",
"Version": "6.3-2",
"Description": "example"
}
]
}
英文:
You could create a stream of booleans, and sort by that. (Use !=
for the same order, ==
for reversed order.)
.vulnerabilities |= sort_by(.Severity != ("CRITICAL","HIGH","MEDIUM","LOW"))
{
"vulnerabilities": [
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "CRITICAL",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "HIGH",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "MEDIUM",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "LOW",
"Version": "6.3-2",
"Description": "example"
}
]
}
答案2
得分: 2
Create a map from strings to numbers; look up the strings and sort by the resulting numbers.
{ "CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3} as $priority_orders |
.vulnerabilities | sort_by($priority_orders[.Severity])
...properly returns, given your input data:
[
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "CRITICAL",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "HIGH",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "MEDIUM",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "LOW",
"Version": "6.3-2",
"Description": "example"
}
]
(You might want to instead sort by a list or tuple, such as [$priority_orders[.Severity], .Product, .Version]
, to come up with data that's usefully ordered when there's more than one thing at each severity level; consider it left as an exercise to the reader).
英文:
Create a map from strings to numbers; look up the strings and sort by the resulting numbers.
{ "CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3} as $priority_orders |
.vulnerabilities | sort_by($priority_orders[.Severity])
...properly returns, given your input data:
[
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "CRITICAL",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "HIGH",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "MEDIUM",
"Version": "6.3-2",
"Description": "example"
},
{
"CVEID": "CVE-2022-29458",
"Product": "ncurses-base",
"Severity": "LOW",
"Version": "6.3-2",
"Description": "example"
}
]
(You might want to instead sort by a list or tuple, such as [$priority_orders[.Severity], .Product, .Version]
, to come up with data that's usefully ordered when there's more than one thing at each severity level; consider it left as an exercise to the reader).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论