Hacker News new | past | comments | ask | show | jobs | submit login
Converting Strings to Objects with Jq (qmacro.org)
9 points by todsacerdoti on May 7, 2022 | hide | past | favorite | 4 comments



Just out of old habits, tend to try do everything first with the core bin tools and then jq. Not saying jq is not useful ...

Command:

  echo "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
  " | column       --json       --table-name SILLY_STRING       --table-columns $(seq --separator=, 1 1)
Output:

  {
     "SILLY_STRING": [
        {"1": "SAP-samples/cloud-sdk-js"},
        {"1": "SAP-samples/cloud-cap-samples-java"},
        {"1": "SAP-samples/btp-setup-automator"},
        {"1": "SAP-samples/btp-ai-sustainability-bootcamp"},
        {"1": "SAP-samples/cloud-cap-samples"},
        {"1": "SAP-samples/ui5-exercises-codejam"},
        {"1": "SAP-samples/cap-sflight"},
        {"1": "SAP-samples/cloud-cf-feature-flags-sample"},
        {"1": "SAP-samples/cloud-espm-cloud-native"},
        {"1": "SAP-samples/iot-edge-samples"}
     ]
  }


(Author of post here) Imposing constraints upon oneself is not a bad thing at all, I enjoy that approach (which was also why this time I constrained myself to `jq`). I didn't know that `column` could do so much, thanks for the pointer, btw.


    seq --separator=, 1 1
That is the longest way I've ever seen of writing "1" in my life. Bonus points: on Linux, it just is "1\n", versus Mac it's "1," (with no trailing newline)

Is there some story to that?


DOH! Sharp eye :) The original was:

  TARGET_FILE=~tomcat/logs/access.log
  tail \
    --lines 1 ${TARGET_FILE} | \
      column \
        --json \
        --table-name ACCESS_LOG \
        --table-columns $(seq --separator=, 1 48)
And the story behind it is a bit silly but memorable. To keep things fun, our ENG team does "left field challenges" and this one was "using only core bin tools, how would you get the field numbers and field values of the tomcat access log file in json format". Its memorable because not long after the code was posted in the ENG chat, we had a PRD incident and some team members used it and found it useful.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: