使用 jq 将递归下降结果收集到单个数组中

Use jq to collect recursive-descent results into a single array

是否可以使用 jqrecursive-descent 个结果收集到一个数组中?

flatten会有帮助吗?在我看来是这样,但我就是无法让它工作。看看我现在在 https://jqplay.org/s/6bxD-Wq0QE 有多远,有人可以让它工作吗?

顺便说一句,

所以现在一切都归结为如何过滤掉那些 null

更新:,来自 "collect into a single array" 我想得到这样的东西:

[
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topics": ["banking", "leumi", "api", "puppeteer", "scraper", "open-api"]
  }
]

而不是在所有不同的返回结果中重复相同的 name。因此递归下降在我看来是一种选择,但只要我能得到如上所示的结果,我对任何解决方案都持开放态度。那可能吗?谢谢。

不确定您期望在结果中得到什么...但您似乎正在尝试将所有存储库及其主题放在一个平面数组中。我看不出您应该在此处使用递归的任何理由,您只是从一个 class 个对象中进行选择。直接引用即可。

[.data.search.edges[].node | {name,topic:(.repositoryTopics.nodes[].topic.topics)}]

对于您的特定输入产生:

[
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": "banking"
  },
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": "leumi"
  },
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": "api"
  },
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": "puppeteer"
  },
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": "scraper"
  },
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": "open-api"
  }
]

https://jqplay.org/s/G2inYAJNLS


如果您想在节点中包含一组主题,只需将选择主题的过滤器放在 [].

中,将它们收集在一个数组中即可
[.data.search.edges[].node | {name,topic:[.repositoryTopics.nodes[].topic.topics]}]
[
  {
    "name": "leumi-leumicard-bank-data-scraper",
    "topic": [
      "banking",
      "leumi",
      "api",
      "puppeteer",
      "scraper",
      "open-api"
    ]
  },
  {
    "name": "echarts-scrappeteer",
    "topic": []
  }
]

https://jqplay.org/s/0AFneNK89i

一种收集 non-falsey 值的方法:

.data.search.edges[].node 
| {name, topics: [.. | .topics? | select(.)]}

结果将是:

{
  "name": "leumi-leumicard-bank-data-scraper",
  "topics": [
    "banking",
    "leumi",
    "api",
    "puppeteer",
    "scraper",
    "open-api"
  ]
}
{
  "name": "echarts-scrappeteer",
  "topics": []
}