1

I've a json data returned by endpoint which is as below:

{
"id":"1383",
"entryPoint":"Stranger in the night.",
"Source":"music"
}

What I need is to get the value associated with the key "entryPoint" displayed along with the default text, to produce something like that:

;; This buffer is for text that is not saved, and for Lisp evaluation.
;; Stranger in the night.
;; To create a file, visit it with C-x C-f and enter text in its buffer.

Maybe it is a silly question, but I'm just staring my adventure with emacs. Every little helps.

2 Answers 2

3

I do something similar to show a random fortune as my scratch message, so I've adapted my code for that (which is also why the commenting part might seem like overkill -- and indeed is if your data will only ever contain single-line values).

(require 'json)
(require 'subr-x)

(defun my-json-scratch-message (jsonfile)
  "Combine `initial-scratch-message' with text from JSONFILE."
  (when-let* ((data (and (file-exists-p jsonfile)
                         (json-read-file jsonfile)))
              (string (alist-get 'entryPoint data)))
    (with-temp-buffer
      (insert initial-scratch-message)
      (goto-char (point-min))
      (forward-line 1)
      (narrow-to-region (point) (point))
      (insert string)
      (insert "\n")
      (let ((comment-start ";;")
            (comment-empty-lines t))
        (comment-region (point-min) (point-max)))
      (delete-trailing-whitespace (point-min) (point-max))
      (widen)
      (buffer-substring-no-properties (point-min) (point-max)))))

;; Set `initial-scratch-message' from `my-json-scratch-message'.
(when-let ((message (my-json-scratch-message "~/scratch.json")))
  (setq initial-scratch-message message))

but what if I have a service at the specific url, and I want to obtain json from there?

(require 'json)
(require 'subr-x)
(require 'url)
(defvar url-http-end-of-headers)
(defvar url-http-response-status)

(defun my-json-scratch-message (url key)
  "Combine `initial-scratch-message' with text for KEY of json from URL."
  (when-let* ((buf (url-retrieve-synchronously url :silent))
              (data (unwind-protect
                        (with-current-buffer buf
                          (and (>= url-http-response-status 200)
                               (< url-http-response-status 300)
                               (goto-char (1+ url-http-end-of-headers))
                               (json-parse-buffer :object-type 'alist)))
                      (kill-buffer buf)))
              (string (alist-get key data)))
    (with-temp-buffer
      (insert initial-scratch-message)
      (goto-char (point-min))
      (forward-line 1)
      (narrow-to-region (point) (point))
      (insert string)
      (insert "\n")
      (let ((comment-start ";;")
            (comment-empty-lines t))
        (comment-region (point-min) (point-max)))
      (delete-trailing-whitespace (point-min) (point-max))
      (widen)
      (buffer-substring-no-properties (point-min) (point-max)))))

;; Set `initial-scratch-message' from `my-json-scratch-message'.
(when-let ((message (my-json-scratch-message
                     "https://jsonendpoint.com/hello/endpoint/"
                     'message)))
  (setq initial-scratch-message message))

;; curl https://jsonendpoint.com/hello/endpoint/
;; {"message":"hello"}
3
  • And where did you put that code? In init.el/.emacs file? Sep 22, 2022 at 7:00
  • Yes, in your init file (whichever of the possible init filenames you're actually using).
    – phils
    Sep 22, 2022 at 7:14
  • phils, but what if I have a service at the specific url, and I want to obtain json from there? Sep 30, 2022 at 16:49
1
(let* ((s "
   {
\"id\":\"1383\",
\"entryPoint\":\"Stranger in the night.\",
\"Source\":\"music\"
}")
       (json.el (with-temp-buffer
          (save-excursion (insert s))
          (json-read))))
  (format "%s %s"
      (alist-get 'entryPoint
             json.el)
      (alist-get 'Source
             json.el)))

When you C-x C-e the above snippet you get

"Stranger in the night. music"

You can set your initial-scratch-message to the above string.


I don't work with javascript or json much. But often times I have to examine json data fetched with curl or wget. So, I have the following in my .emacs. The snippet pretty prints any .json file that you may open; works well for "not so large" .json files.

(define-derived-mode json-mode javascript-mode "json-mode")

(setq auto-mode-alist
      `(
    ,@`(
        ("\\.json\\'" . json-mode))
    ,@auto-mode-alist))

(add-hook 'json-mode-hook #'json-pretty-print-buffer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.