Skip to content

Instantly share code, notes, and snippets.

@5hanth
Last active November 16, 2019 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5hanth/1486ecf583845812f319 to your computer and use it in GitHub Desktop.
Save 5hanth/1486ecf583845812f319 to your computer and use it in GitHub Desktop.
Parse text of links to json in elisp
{
"links": [
{
"href": "http:\/\/github.com",
"label": "Github"
},
{
"href": "http:\/\/stackoverflow.com",
"label": "Stackoverflow"
},
{
"href": "http:\/\/twitter.com",
"label": "Twitter"
},
{
"href": "http:\/\/google.com",
"label": "Google"
}
]
}#
; emacs -batch -l text-to-json.el --eval='(txt-to-json "out.json" "to-parse.txt")'
(require 'cl)
(require 'json)
(defun txt-to-json (&optional out-file src-file)
(let* ((current-buf (buffer-file-name))
(parsed (with-temp-buffer
(insert-file-contents (or src-file current-buf))
(split-string
(buffer-substring-no-properties
(point-min) (point-max))
"\n"))))
(with-temp-buffer
(insert
(json-encode `(:links
,(loop for label in parsed
by #'cddr
for href in (cdr parsed)
by #'cddr
collect (list (cons :label label)
(cons :href href))))))
(json-pretty-print (point-min)
(point-max))
(if out-file
(write-region nil nil out-file)
(buffer-string)))))
Github
http://github.com
Stackoverflow
http://stackoverflow.com
Twitter
http://twitter.com
Google
http://google.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment