Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[New Blog Post] Surely you're jqing #8

Open
stochastical opened this issue Feb 18, 2025 · 1 comment
Open

[New Blog Post] Surely you're jqing #8

stochastical opened this issue Feb 18, 2025 · 1 comment
Labels
new-blog-post Triggers a GitHub Action to add a new blog post

Comments

@stochastical
Copy link
Owner

Blog Post Title

Surely you're jqing

Blog Section

Microblog

Post Date

2025-02-18

Description

Learning jq by reading the manual end-to-end.

Link

https://jqlang.org/manual/

Additional Content

Today I read through the jq manual cover-to-cover. For those unaware, jq is a popular CLI tool to query and manipulate JSON. It's a mini-language with nice functional semantics and fits into the ethos of composable CLI tools.

It was an exemplar of well-written technical documentation. Concise, well-written, littered with examples, and linking to an interactive playground to test-and-learn.

Some learnings:

  1. It's surprisingly functional! You can implement recursive functions and use higher-order functions! For example, here's factorial in jq:
jq '[.,1]|until(.[0] < 1; [.[0] - 1, .[1] * .[0]])|.[1]'
  1. It supports string interpolation - this is really nice if you're piping stuff from JSON into a string. Coupled with format strings this becomes frictionless:
$ echo '{"search":"hello; world"}' | jq -r '@uri "https://www.google.com/search?q=\(.search)"'
# https://www.google.com/search?q=hello%3B%20world
  1. You can define functions that accept functions1, and control structures that allow labelling.
echo '[[1,2],[10,20]]' | jq -r 'def addvalue(f): . + [f]; map(addvalue(.[0]))'
[[1,2,1], [10,20,10]]
  1. You can traverse complex data structures with first-class pathing support. And you can easily modify nested structures to extend objects.

Side note: This is one of my goals for 2025 - read through documentation end-to-end to develop mastery over tools. I'm trying to prioritise selectively depth over breadth.

Footnotes

  1. The line between "functions" and "filters" is a little blurry to me.

@stochastical stochastical added the new-blog-post Triggers a GitHub Action to add a new blog post label Feb 18, 2025
Copy link
Contributor

Added new blog post to content/micro-blog/2025-02-18-surely-youre-jqing.md

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
new-blog-post Triggers a GitHub Action to add a new blog post
Projects
None yet
Development

No branches or pull requests

1 participant