I wanted to use jq in org-mode to query JSON, and saw a jq-mode that supports org-babel:
jq-mode provides ob-jq for working with literate programming in Org mode.
Add jq under org-babel-load-languages in your .emacs file
(org-babel-do-load-languages 'org-babel-load-languages
'((jq . t)))
I changed my org-mode configuration, but I am not sure how to use the jq support. There are no usage examples in the documentation. I guess I am missing something obvious due to my lack of knowledge about how code blocks work.
In the command line, jq should be used as:
$ cat a.json | jq '.[] | [.id]'
[
1
]
[
2
]
, where a.json
is a JSON file with the following content:
[
{
"id": 1,
"details": {
"username": "jamesbrown",
"name": "James Brown"
}
}
, {
"id": 2,
"details": {
"username": "johnsmith",
"name": "John Smith"
}
}
]
In my initial attempt to do this query in org-mode, I added the following block with the jq filter .[] | [.id]
in it:
#+begin_src jq
.[] | [.id]
#+end_src
Then, I don't know how to provide the JSON input to jq. If I press C-c C-c
on the code block, I just get an empty:
#+RESULTS:
My questions are:
How can I provide input or more generally, what is the intended way to use jq in org?
*How can I save the result JSON or text string from jq to a file?
(Ideally, I'd like to have the input to be some data/code block embedded inside the same org file, for readability)