Ir al contenido principal

On generating JSON content from EMACS

Context

Hi, probably this post work for someone else. I wanted to gain literacy in ASP.NET development but got stuck at

episode 9 of 13, 'Enhancing your Web API', of 'What is ASP.NET' tutorial series when at trying to generate a json file to feed my web application project with content. So after have a read through the json.el library I came up with the following helper functions, not sure about there is better way of doing it as once done I still have to wrap the result with '[' and ']' (as I couldn't manage to make concatenate it) I would appreciate any comment. Here it goes:

Step One, the Expected Output

  1. As seen on the screenshoot:

nil

but my SPA goes about fruits and vegetables items, so following the structure show below:

[
 {
 "id" : "item",
 "img": "path-name.png|jpg",
 "info": "wolframalphaLink",
 },
 {..
 ..},
 {..
 ..},
 ... 
 ]

In order to call my object list easily, lets set a variable 'fruts':

(setq fruts '(beetroots carrots cucumber celery cougette kale apple kiwifruit avocado lemon pepper garlic ginger sweet potatoes banana orange mandarin blackberry))

Step Two, lets generate the block code for every item in my object list

In order to generate the data block for every item

(defun foo-objs-list (x)
  (mapcar (lambda (i)
            (split-string
             (format "id %s img img/%s info https://www.wolframalpha.com/input/?i=%s" i i i))) x))

Step Three, Now lets encode every item block in json terms.

  1. In order to mapcar every block in json terms:
(defun foo-encoding-objs-list (x)
  (mapcar (lambda (i)
            (insert
             (format "%s,"
                     (json-encode-plist i))))
          (foo-objs-list x)))

Step Four, Just use it!

(foo-encoding-objs-list fruts)
  1. Or the version combining both preceding functions in one:
(defun foo-encoding-objs-list-version-all-together (x)
   (mapcar (lambda (i)
             (insert (format "%s," (json-encode-plist i))))
           (mapcar (lambda (i)
                     (split-string
                      (format
                       "id %s img img/%s info https://www.wolframalpha.com/input/?i=%s "
                              i i i))) x)))