Search code examples
jsonjqcollectrecursive-descent

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


Is it possible to collect recursive-descent results into a single array with jq?

Would flatten help? Looks so to me, but I just cannot get it working. Take a look how far I am now at https://jqplay.org/s/6bxD-Wq0QE, anyone can make it working?

BTW,

  • .data.search.edges[].node | {name, topics: ..|.topics?} works, but I want all topics from the same node to be in one array, instead of having same name in all different returned results.
  • flatten alone will give me Cannot iterate over null, and
  • that's why I'm trying to use map(select(.? != null)) to filter the nulls out. However, I'd get Cannot iterate over null as well for my map-select.

So now it all comes down to how to filter out those nulls?

UPDATE:, by "collect into a single array" I meant to get something like this:

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

instead of having same name duplicated in all different returned results. Thus recursively descends seems to me to be the option, but I'm open to any solution as long as I can get result like above. Is that possible? Thx.


Solution

  • One way to collect the non-falsey values:

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

    The result would be:

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