Function: verb-json-get
verb-json-get is a natively compiled function defined in verb.el.
Signature
(verb-json-get TEXT &rest PATH)
Documentation
Interpret TEXT as a JSON object and return value under PATH.
The outermost JSON element in TEXT must be an object. PATH must be a list of strings, symbols (which will be converted to strings), or integers. The PATH list will be traversed from beginning to end, using each item to access a sub-value in the current JSON element (and setting the current JSON element to that new value). This is supposed to work in a similar way JSONPath does, more info at URL https://goessner.net/articles/JsonPath/.
For example, for the following TEXT:
{
"test": {
"foo": ["apples", "oranges"]
}
}
Using PATH ("test" "foo" 1) will yield "oranges".
Source Code
;; Defined in /nix/store/8jnfn8w2mxcxbr2crbklcnija9m0zlkq-emacs-packages-deps/share/emacs/site-lisp/elpa/verb-20260818.1921/verb.el
(defun verb-json-get (text &rest path)
"Interpret TEXT as a JSON object and return value under PATH.
The outermost JSON element in TEXT must be an object.
PATH must be a list of strings, symbols (which will be converted to
strings), or integers. The PATH list will be traversed from beginning
to end, using each item to access a sub-value in the current JSON
element (and setting the current JSON element to that new value).
This is supposed to work in a similar way JSONPath does, more info at
URL `https://goessner.net/articles/JsonPath/'.
For example, for the following TEXT:
{
\"test\": {
\"foo\": [\"apples\", \"oranges\"]
}
}
Using PATH (\"test\" \"foo\" 1) will yield \"oranges\"."
(unless path
(user-error "%s" "No path specified for JSON value"))
(let ((obj (json-read-from-string text)))
(dolist (key path)
(setq
obj
(cond
(;; Path element is a string (or symbol).
(or (stringp key) (symbolp key))
(when (symbolp key)
(setq key (symbol-name key)))
;; Obj may be an alist, plist or hash-table.
(pcase json-object-type
('alist
(cdr (assoc-string key obj)))
('plist
(plist-get obj (intern (concat ":" key))))
('hash-table
(gethash key obj))
(_
(user-error "%s" "Unknown value for `json-object-type'"))))
(;; Path element is an integer.
(integerp key)
;; Handle negative indexes by adding the negative index to the size of
;; the sequence, and using that as the new index.
(when (and (< key 0) (seqp obj))
(setq key (+ key (length obj))))
;; Obj may be a list or a vector.
(pcase json-array-type
('list
(nth key obj))
('vector
(aref obj key))
(_
(user-error "%s" "Unknown value for `json-array-type'"))))
(;; Invalid key type.
t
(user-error "Invalid key: %s" key)))))
obj))