1. 7
  1. 2

    It’s amazing how the brain works, especially when asleep. I woke up in the middle of last night, suddenly realising I could improve the first jq expression in my solution. I hadn’t even been (consciously) thinking about the post at all.

    I’ve updated the post with a small section at the end that describes going from [.,inputs] to [inputs] with the --null-input option.

    1. 2

      I think it can be golfed a bit further at the cost of some readability.

      jq -Rn '[inputs|{"name":sub("^.+/";"")}]'

      rjp@CyanistesCyanus ~ $ cat names.txt
      SAP-samples/cloud-sdk-js
      SAP-samples/cloud-cap-samples-java
      SAP-samples/btp-setup-automator
      SAP-samples/btp-ai-sustainability-bootcamp
      SAP-samples/cloud-cap-samples
      SAP-samples/ui5-exercises-codejam
      SAP-samples/cap-sflight
      SAP-samples/cloud-cf-feature-flags-sample
      SAP-samples/cloud-espm-cloud-native
      SAP-samples/iot-edge-samples
      rjp@CyanistesCyanus ~ $ jq -cRn '[inputs|{"name":sub("^.+/";"")}]' < names.txt
      [{"name":"cloud-sdk-js"},{"name":"cloud-cap-samples-java"},{"name":"btp-setup-automator"},{"name":"btp-ai-sustainability-bootcamp"},{"name":"cloud-cap-samples"},{"name":"ui5-exercises-codejam"},{"name":"cap-sflight"},{"name":"cloud-cf-feature-flags-sample"},{"name":"cloud-espm-cloud-native"},{"name":"iot-edge-samples"}]
      
      1. 3

        Lovely! It’s funny, I feel as thought I’m on the cusp of golfing here, but not quite. That’s not the aim of this post (or the one I’ve just written), though - it’s more to explain the filter, with the hope that there’s some demystification for some folks that might lead to them trying jq.

      2. 2

        I enjoy reading posts about jq so thought others might too, hence this submission. It sort of also relates to the recent submission about jq and qz which also inspired a post. Thanks.

        1. 3

          I love seeing folks write in jq, whether it’s entire modules or short one-line programs. It’s one of the few languages which makes me feel hope for our profession, and it’s becoming a standard tool which distros include by default. Thanks for sharing your experience.

          1. 2

            This is a bit meta, but I was wondering - would you folks have any objection in me sharing the main post now here on Lobsters, for which this post was a sort of “prequel”? I’ve just finished it, and it goes into more detail and includes further explorations of jqs functions….

        2. 1

          Here’s the solution that I prefer (even though it’s not pure jq):

          sed -E 's:^.+/::' names.txt | jq -R | jq -s 'map({ name: . })'