JQ 按值排序

huangapple go评论53阅读模式
英文:

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"
    }
  ]
}

Demo

答案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).

huangapple
  • 本文由 发表于 2023年6月15日 01:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定