Function: json-path-to-position

json-path-to-position is a byte-compiled function defined in json.el.gz.

Signature

(json-path-to-position POSITION &optional STRING)

Documentation

Return the path to the JSON element at POSITION.

When STRING is provided, return the path to the position in the string, else to the position in the current buffer.

The return value is a property list with the following properties:

:path -- A list of strings and numbers forming the path to
                the JSON element at the given position. Strings
                denote object names, while numbers denote array
                indices.

:match-start -- Position where the matched JSON element begins.

:match-end -- Position where the matched JSON element ends.

This can, for instance, be useful to determine the path to a JSON element in a deeply nested structure.

Source Code

;; Defined in /usr/src/emacs/lisp/json.el.gz
(defun json-path-to-position (position &optional string)
  "Return the path to the JSON element at POSITION.

When STRING is provided, return the path to the position in the
string, else to the position in the current buffer.

The return value is a property list with the following
properties:

:path        -- A list of strings and numbers forming the path to
                the JSON element at the given position.  Strings
                denote object names, while numbers denote array
                indices.

:match-start -- Position where the matched JSON element begins.

:match-end   -- Position where the matched JSON element ends.

This can, for instance, be useful to determine the path to a JSON
element in a deeply nested structure."
  (save-excursion
    (unless string
      (goto-char (point-min)))
    (let* ((json--path '())
           (json-pre-element-read-function #'json--record-path)
           (json-post-element-read-function
            (lambda () (json--check-position position)))
           (path (catch :json-path
                   (if string
                       (json-read-from-string string)
                     (json-read)))))
      (when (plist-get path :path)
        path))))