0

In Elisp, I want to insert the contents of the "gazonk" JSON block into a variable.

#+name: gazonk
#+begin_src json
{
  xyzzy "
}
#+end_src

, but I just get "End of file during parsing"

#+BEGIN_SRC elisp :noweb yes
(setq example-data "<<gazonk>>")
#+END_SRC

Obviously, it's not valid JSON, but I want to insert the verbatim contents;)

EDIT: Here is a better example:

Here's valid JSON

#+name: gazonk
#+begin_src json
{
  "GivenName" : "Mortimer",
  "SurName" : "Smith",
  "NameSuffixText" : "Sr",
  "PreferredName" : "Morty",
}
#+end_src

Here's valid JSON, but enclosed, so not really valid;)

#+name: gazonk_enclosed
#+begin_src json
"
{
  "GivenName" : "Mortimer",
  "SurName" : "Smith",
  "NameSuffixText" : "Sr",
  "PreferredName" : "Morty",
}
"
#+end_src

This doesn't work with valid json

#+BEGIN_SRC elisp :results verbatim drawer :noweb yes
  (setq data <<gazonk>>)
#+END_SRC

, but this works

#+BEGIN_SRC elisp :results verbatim drawer :noweb yes
  (setq data <<gazonk_enclosed>>)
#+END_SRC

It's a general problem. I can't insert a code block into a variable, no matter what the language is. Here's XML:

#+name: fd7df9594ccfe7889b7e5817067733efc038967053e0ceb66b33bdc0ad7390d8
#+begin_src xml
<xml>
  <table>
    <rec id="1">
      <numField>123</numField>
      <stringField>String Value</stringField>
    </rec>
    <rec id="2">
      <numField>346</numField>
      <stringField>Text Value</stringField>
    </rec>
    <rec id="3">
      <numField>-23</numField>
      <stringField>stringValue</stringField>
    </rec>
  </table>
</xml>
#+end_src

#+BEGIN_SRC elisp :noweb yes
(setq example-data "<<fd7df9594ccfe7889b7e5817067733efc038967053e0ceb66b33bdc0ad7390d8>>")
#+END_SRC

5
  • How are you evaluating JSON blocks exactly?
    – NickD
    Aug 12, 2023 at 3:24
  • You seem to have loaded some Org babel library for JSON: when I try to evaluate a JSON source code block, I get an error: No babel-execute function for json!. And what works for you does not work for me. So can you please answer the question in my previous comment?
    – NickD
    Aug 12, 2023 at 16:45
  • 1
    The problem is I'm not sure I understand. Blocks with JSON and XML are not evaluated. At least, I'm not evaluating them for now. I want to include the verbatim contents of a JSON or XML block into a variable, cause I'm using this JSON for example as a body in a curl request. Aug 12, 2023 at 17:37
  • You are right - I think there is a bug with :noweb-prefix . E.g. I tried doing the multiline example towards the bottom of the page in Noweb Reference Syntax and I get no joy: C-c C-v C-v does not show anything different with or without the :noweb-prefix` header (although you should try setting it to no and see if that works for you - it may be version dependent). However even with that fixed, I don't think there is a way to escape the inner double quotes, so that Lisp can parse the string properly.
    – NickD
    Aug 13, 2023 at 18:13
  • Can I somehow create a new block of any kind, where I quote the json block, before I noweb it into something else?;) Aug 14, 2023 at 7:21

1 Answer 1

1

The function (org-babel-expand-noweb-references info) returns the noweb-expanded string for the source block info. The structure of the source block info is described in the help of org-babel-get-src-block-info.

The only thing we need from the info for expanding of the noweb-references is (language body):

#+BEGIN_SRC emacs-lisp
(let* ((json-string (org-babel-expand-noweb-references '(emacs-lisp "<<gazonk>>"))))
  json-string)
#+END_SRC

#+RESULTS:
: {
:   "GivenName" : "Mortimer",
:   "SurName" : "Smith",
:   "NameSuffixText" : "Sr",
:   "PreferredName" : "Morty",
: }

Disclaimer: This is certainly not the intended use of org-babel-expand-noweb-references. So be aware of potential changes in the API.

Maybe, you can ask the people from Orgmode whether this behavior could be held stable or how to tackle your problem differently.

1
  • Ok, that's just pefect;). Thank you. Can I ask if you know how to do it with "src shell" bash as well, now that I got you here? Feb 21 at 11:23

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.