1

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)

1 Answer 1

1

For input, you can use the :in-file header:

#+begin_src jq :results drawer :in-file a.json
  .[] | [.id]
#+end_src

For input and output, you can specify a :cmd-line header:

#+begin_src jq :results drawer :cmd-line < a.json > b.json
   .[] | [.id]
#+end_src

It can also accept a :var param through which you can pass arguments, but I don't know enough about jq to tell you how to use it: it uses the --arg command line switch however - maybe you know how to use that switch and can answer that part of the question?

I got that much by reading ob-jq.el, guessing and experimenting; but it does need proper documentation. Maybe you can write some and add it to Worg? Or submit it to the GitHub site?

3
  • Thanks for the correction! My turn: it's not :cmdline, it's :cmd-line. What's your OS? The I/O redirections might not work on Windows, but they should work on anything Unix-like (including WSL on Windows).
    – NickD
    Commented May 21 at 13:33
  • By the way, I only use jq in the basic way with stdin/stdout in the unix pipe line. I was hoping to simulate it that way: a json block in, a json block out. Not sure if JSON block is a thing in org. emacs.stackexchange.com/questions/68125
    – tinlyx
    Commented May 21 at 20:17
  • ". I wasn't sure where the docs for in-file, cmd-line are" - they aren't. As I mentioned, I found them by reading the ob-jq.el code.
    – NickD
    Commented May 21 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.