From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: Feature request: results type json Date: Wed, 28 Mar 2018 15:30:14 -0700 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a113ca360195ffc0568808de9" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:60358) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f1JaE-0007xK-Sy for emacs-orgmode@gnu.org; Wed, 28 Mar 2018 18:30:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f1JaD-0006Q0-H5 for emacs-orgmode@gnu.org; Wed, 28 Mar 2018 18:30:18 -0400 Received: from mail-wr0-x235.google.com ([2a00:1450:400c:c0c::235]:39728) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1f1JaD-0006Pa-6S for emacs-orgmode@gnu.org; Wed, 28 Mar 2018 18:30:17 -0400 Received: by mail-wr0-x235.google.com with SMTP id c24so3654574wrc.6 for ; Wed, 28 Mar 2018 15:30:17 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Ag Ibragimov Cc: org-mode-email --001a113ca360195ffc0568808de9 Content-Type: text/plain; charset="UTF-8" If you can get ob-sql to output data in tabular form (if it is not nested) you can do something like this: #+name: tabular #+BEGIN_SRC python :results value d = [['type', 'test'], ['format', 'json']] return d #+END_SRC #+RESULTS: tabular | type | test | | format | json | #+BEGIN_SRC emacs-lisp :var data=tabular :wrap json (json-encode data) #+END_SRC #+RESULTS: #+BEGIN_json {"type":["test"],"format":["json"]} #+END_json If the data is nested, then you can see if there is a way to output a lisp readable string: #+name: my-data #+BEGIN_SRC python :results output print('((type . test) (format . json))') #+END_SRC #+RESULTS: my-data : ((type . test) (format . json)) #+BEGIN_SRC emacs-lisp :var data=my-data :wrap json (json-encode (read data)) #+END_SRC #+RESULTS: #+BEGIN_json {"type":"test","format":"json"} #+END_json John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Wed, Mar 28, 2018 at 2:19 PM, wrote: > Hmm, it works for emacs-lisp, however I specifically wanted to use it with > ob-sql, and can't find a way. > > On Wed, Mar 28, 2018 at 2:05 PM John Kitchin > wrote: > >> Does this do what you want: >> >> #+BEGIN_SRC emacs-lisp :wrap json >> (json-encode '((type . "text"))) >> #+END_SRC >> >> #+RESULTS: >> #+BEGIN_json >> {"type":"text"} >> #+END_json >> >> John >> >> ----------------------------------- >> Professor John Kitchin >> Doherty Hall A207F >> Department of Chemical Engineering >> Carnegie Mellon University >> Pittsburgh, PA 15213 >> 412-268-7803 <(412)%20268-7803> >> @johnkitchin >> http://kitchingroup.cheme.cmu.edu >> >> >> On Wed, Mar 28, 2018 at 1:42 PM, Ag Ibragimov >> wrote: >> >>> >>> Sorry, I don't know the best medium to convey ideas and I'm afraid not >>> qualified (familiar with org-mode codebase) to submit a PR for this. >>> I wonder how difficult would be to >>> add the possibility to have babel src block results to be rendered as >>> json? >>> Basically a new result type https://orgmode.org/manual/ >>> results.html#results >>> >>> Thank you. >>> >>> >> --001a113ca360195ffc0568808de9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
If you can get ob-sql to output data in tabular form (if i= t is not nested) you can do something like this:

#+= name: tabular
#+BEGIN_SRC python :results value
d =3D [= ['type', 'test'], ['format', 'json']]
=
return d
#+END_SRC

#+RESULTS: tabul= ar
| type=C2=A0 =C2=A0| test |
| format | json |
<= div>
#+BEGIN_SRC emacs-lisp :var data=3Dtabular :wrap json
(json-encode data)
#+END_SRC

#+R= ESULTS:
#+BEGIN_json
{"type":["test"= ;],"format":["json"]}
#+END_json
<= div>
If the data is nested, then you can see if there is a wa= y to output a lisp readable string:

#+name: m= y-data
#+BEGIN_SRC python :results output
print('((= type . test) (format . json))')
#+END_SRC

#+RESULTS: my-data
: ((type . test) (format . json))
<= div>


#+BEGIN_SRC emacs-lisp :va= r data=3Dmy-data :wrap json
(json-encode (read data))
#= +END_SRC

#+RESULTS:
#+BEGIN_json
{"type":"test","format":"json"}<= /div>
#+END_json



Jo= hn

-----------------------------------
Professor John Kitchin=C2= =A0
Doherty Hall A207F
Department of Chemical Engineering
Carnegie= Mellon University
Pittsburgh, PA 15213
412-268-7803
=

On Wed, Mar 28, 2018 at 2:19 PM, <agzam.ibragimov@gmail.com> wrote:
Hmm, it works for emacs-lisp, however I specifical= ly wanted to use it with ob-sql, and can't find a way.=C2=A0=C2=A0

On Wed, Mar 28, 2018 at 2:05 PM John Kitchin <jkitchin@andrew.cmu.edu> wrote:

<= /div>

On Wed, Mar 28, 2018 at 1:42 PM, Ag Ibragimo= v <agzam.ibragimov@gmail.com> wrote:

Sorry, I don't know the best medium to convey ideas and I'm afraid = not
qualified (familiar with org-mode codebase) to submit a PR for this.
I wonder how difficult would be to
add the possibility to have babel src block results to be rendered as
json?
Basically a new result type https://orgmode.org/manua= l/results.html#results

Thank you.



--001a113ca360195ffc0568808de9--