From mboxrd@z Thu Jan 1 00:00:00 1970 From: stardiviner Subject: Re: Feature request: results type json Date: Thu, 29 Mar 2018 11:44:32 +0800 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="------------9F5887E5CC1C9E28A341B0F2" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55949) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f1OUT-00006L-Eh for emacs-orgmode@gnu.org; Wed, 28 Mar 2018 23:44:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f1OUP-0001gw-Dt for emacs-orgmode@gnu.org; Wed, 28 Mar 2018 23:44:41 -0400 Received: from mail-pg0-x22d.google.com ([2607:f8b0:400e:c05::22d]:46239) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1f1OUP-0001gp-46 for emacs-orgmode@gnu.org; Wed, 28 Mar 2018 23:44:37 -0400 Received: by mail-pg0-x22d.google.com with SMTP id t12so2222502pgp.13 for ; Wed, 28 Mar 2018 20:44:36 -0700 (PDT) Received: from [0.0.0.0] ([119.28.89.243]) by smtp.gmail.com with ESMTPSA id l90sm11330207pfg.5.2018.03.28.20.44.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Mar 2018 20:44:35 -0700 (PDT) In-Reply-To: Content-Language: en-US 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: emacs-orgmode@gnu.org This is a multi-part message in MIME format. --------------9F5887E5CC1C9E28A341B0F2 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit You can wrap result value into another language src block like this: #+begin_src ruby :results code :wrap src shell puts "echo 'hello'" #+end_src #+RESULTS: #+begin_src shell echo 'hello' #+end_src Same for JSON by changed `:wrap src shell` into `wrap json`. But you need the value is JSON. Isaw you mentioned want ob-sql support JSON return type. I guess it need to be implemented in ob-sql.Or you can use `:post` to convert result to JSON. Or use `advice-add` to add a function to convert result to JSON. On 03/29/2018 06:30 AM, John Kitchin wrote: > 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 > @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. > > > --------------9F5887E5CC1C9E28A341B0F2 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

You can wrap result value into another language src block like this:

#+begin_src ruby :results code :wrap src shell
puts "echo 'hello'"
#+end_src

#+RESULTS:
#+begin_src shell
echo 'hello'
#+end_src

Same for JSON by changed `:wrap src shell` into `wrap json`. But you need the value is JSON.

I saw you mentioned want ob-sql support JSON return type. I guess it need to be implemented in ob-sql. Or you can use `:post` to convert result to JSON. Or use `advice-add` to add a function to convert result to JSON.


On 03/29/2018 06:30 AM, John Kitchin wrote:
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

On Wed, Mar 28, 2018 at 2:19 PM, <agzam.ibragimov@gmail.com> 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 <jkitchin@andrew.cmu.edu> 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

On Wed, Mar 28, 2018 at 1:42 PM, Ag Ibragimov <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/manual/results.html#results

Thank you.




--------------9F5887E5CC1C9E28A341B0F2--