1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# Traverse complex JSON structures with live feedback
Zen Monk Alain M. Lafon
[[!template id=vid src="https://mirror.csclub.uwaterloo.ca/emacsconf/2020/emacsconf-2020--25-traverse-complex-json-structures-with-live-feedback-counsel-jq--zen-monk-alain-m-lafon.webm"]]
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 from the pad
### 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?+1
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 from the pad
- 200ok GmbH (<https://200ok.ch>)
- Play Emacs like an instrument: <https://www.youtube.com/watch?v=gfZDwYeBlO4>
- jq: <https://stedolan.github.io/jq/>
- ivy supports dynamic sources. So does helm I guess.
- counsel-jq: <https://github.com/200ok-ch/counsel-jq>
- organice: <https://github.com/200ok-ch/organice>
- Entire presentation inside Emacs, with a count down reminder.
|