emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

small seq function for json data


From: Stefan Huchler
Subject: small seq function for json data
Date: Sun, 16 Oct 2016 07:29:37 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Hello,

accessing data coming from json requests can be a little bit anoying
in elisp I think.

So inspired by let-alist I wrote a more powerful version that supports
also vector which is also used in json messages.

(setq x '((foo . [((bar . "string of interest"))])))

(defun sbit-seq-get (seq path)
  (cond ((null path) seq)
         ((listp seq)
          (sbit-seq-get (cdr (assoc (car path) seq)) (cdr path))
          )
        ((vectorp seq)
         (sbit-seq-get (elt seq (car path)) (cdr path))
         )
        (t seq)))

(sbit-seq-get x '(foo 0 bar))

alternative manuall access would look like that:
(cdr (assoc 'bar (elt (cdr (assoc 'foo x))0)))

or with alist-get:
(let-alist (elt (let-alist x .foo) 0 ) .bar)


Does that make sense? Maybe integrate something similar to seq.el?
Eventualy plist support could maybe added, too. I dont know :)

I have more of a python background there its also fine to mix such keys
like:

x["foo"][0]["bar"]

Also I find it pretty inconsistent that elt as example uses the key
after the sequence also let-alist, but assoc and nth has first the key
as parameter, key first makes more sense in my oppinion (but) maybe I
am to bad with paredit to jump to the end of the parens :)

Well shure it would be a pain in the ass to change all addons so
I guess we have to live with that :)

Any thoughts on that?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]