Back to the schedule
Previous: Analyze code quality through Emacs: a smart forensics approach and the story of a hack
Next: NonGNU ELPA

Traverse complex JSON structures with live feedback

Zen Monk Alain M. Lafon

Download video

Download compressed .webm video (15.8M)

If you are working with complex nested JSON structures, you are probably familiar with jq which is like sed for JSON data and great at what it does. However, being a command-line tool like sed, the feedback for writing queries and seeing their results is a discrete process and not live.

When working with Emacs, we are used to good auto-completion and live feedback. Formerly, this was mostly done with static input, but with modern completion frameworks like Ivy and Counsel, this can be done with dynamic inputs, as well.

counsel-jq is a package with which you can quickly test queries and traverse a complex JSON structure whilst having live feedback. Just call M-x counsel-jq in a buffer containing JSON, then start writing your jq query string and see the output appear live in the message area. Whenever you're happy, hit RET and the results will be displayed to you in the buffer *jq-json*.

In this lightning talk, I'll give a quick overview on how to use counsel-jq and how to build similar completion functionality.

Resources

https://200ok.ch/posts/2020-11-30_emacsconf_traverse_complex_json_structures_with_live_feedback_with_counseljq.html

Questions

Q4: Any plans for counsel-yq and/or -xq? ;-)

counsel-jq currently just shells out to jq. Adding tools build on top of jq (at least yq is afaik) would be very easy. We could employ a strategy pattern to find the right tool based on the current major-mode with a configurable fallback. Here's the place where the shellout happens: https://github.com/200ok-ch/counsel-jq/blob/master/counsel-jq.el#L23

Would you be interested in making a PR for that? (;

Q3: Why repository_url did not autocomplete in addition to the result (I know that it is ivy thing but possible to configure?)

There's no autocompletion for the search query, but that would be a great addition. That would theoretically be possibe by employing jq to look ahead in the current tree and providing options for autocomplete. I'm not certain if Ivy does have autocomplete for search queries, though.

If somebody has more knowledge on that and would like to ping me up or provide a (draft) PR, I'd be happy to help out in that endeavour!

Q2: Is it difficult to provide autocompletion for the JSON query in the minibuffer?

Good question. I'd be curious, too. It's the same question as Q2 where I went into a possibe scenario.

Q1: Is it possible to search in arbitrary deep objects? E.g., an AST represented in JSON.

counsel-jq uses jq under the hood, so all queries that are valid queries in jq should be valid in jq. Hence, I'm inclined to say 'yes' (;

Notes

Sunday, Nov 29 2020, ~11:12 AM - 11:22 AM EST
Sunday, Nov 29 2020, ~ 8:12 AM - 8:22 AM PST
Sunday, Nov 29 2020, ~ 4:12 PM - 4:22 PM UTC
Sunday, Nov 29 2020, ~ 5:12 PM - 5:22 PM CET
Monday, Nov 30 2020, ~12:12 AM - 12:22 AM +08

Back to the schedule
Previous: Analyze code quality through Emacs: a smart forensics approach and the story of a hack
Next: NonGNU ELPA