Elisp: JSON
What is JSON
Import. JSON String to Lisp Object
json-parse-string-
(json-parse-string STRING &rest ARGS)Convert json string to emacs lisp data structure object.
ARGS specify what JSON types convert to what lisp types.
:array-type OBJ- default is
'array. Can be'list. :object-type OBJ- default is
'hash-table. Can be'alist,'plist :null-object OBJ- default is
:null. :false-object OBJ- default is
:false.
;; example of json array (json-parse-string "[1,2,3]") ;; [1 2 3] ;; example of json object (json-parse-string "{ \"name\": \"Mary\", \"age\": 24}") ;; #s(hash-table test equal data ("name" "Mary" "age" 24)) ;; example of json, mixed array and object (json-parse-string "[1, { \"name\": \"Mary\"} ]") ;; [1 #s(hash-table test equal data ("name" "Mary"))] ;; example of json special values (json-parse-string "[true, false, null]") ;; [t :false :null]
Example. Specify Object Type
;; json with duplicate keys (setq xjson "{ \"a\": 3, \"b\": 4, \"b\": 5}") ;; default to hashtable. last key override (json-parse-string xjson) ;; #s(hash-table test equal data ("a" 3 "b" 5)) ;; to associative list. duplicate keys remain (json-parse-string xjson :object-type 'alist) ;; ((a . 3) (b . 4) (b . 5)) ;; to property list. duplicate keys remain (json-parse-string xjson :object-type 'plist) ;; (:a 3 :b 4 :b 5)
Example. Specify Array Type
(json-parse-string "[1,2,3]") ;; [1 2 3] ;; to vector (json-parse-string "[1,2,3]" :array-type 'array) ;; [1 2 3] ;; to list (json-parse-string "[1,2,3]" :array-type 'list) ;; (1 2 3)
Example. Special JSON values, true, false, null
;; by default, ;; json true become t ;; json false become :false ;; json null become :null (json-parse-string "[true, false, null]") ;; [t :false :null] ;; specify how json false and null are converted (json-parse-string "[true, false, null]" :false-object "f" :null-object 0) ;; [t "f" 0]
Import a JSON File
json-parse-buffer-
(json-parse-buffer &rest ARGS)Read JSON string from current buffer at that buffer's current cursor position.
Rest args same as
json-parse-string;; import a json file (setq my-input-filepath "~/xx.json") ;; sample file content is ["a","🌞"] (setq my-imported-obj (with-temp-buffer (insert-file-contents my-input-filepath) (goto-char (point-min)) (json-parse-buffer))) my-imported-obj ;; ["a" "🌞"]
JSON. Export
json.el (old)
library json.el is from
Emacs 23 (date 2009-07)
.
some of its functions are
json-encode
(json-encode [1 2 3]) ;; "[1,2,3]" ;; association list. symbol keys (json-encode (list (cons 'aa 23) (cons 'bb 24) (cons 'cc 33)) ) ;; "{\"aa\":23,\"bb\":24,\"cc\":33}" ;; association list. string keys (json-encode (list (cons "aa" 23) (cons "bb" 24) (cons "cc" 33)) ) ;; "{\"aa\":23,\"bb\":24,\"cc\":33}" ;; property list. symbol keys (json-encode (list :aa 23 :bb 24 :cc 33)) ;; "{\"aa\":23,\"bb\":24,\"cc\":33}" ;; property list. string keys (json-encode (list "aa" 23 "bb" 24 "cc" 33)) ;; "[\"aa\",23,\"bb\",24,\"cc\",33]"
json-read-from-stringjson-read-file
it still have some useful functions such as
json-pretty-print-bufferjson-pretty-print
History
the functions
json-parse-stringjson-parse-bufferjson-serializejson-insert
- Are written in C.
- In Emacs 30 (date 2025), they are not dependent on external library.
- In Emacs 27 (date 2020). They dependent on external C library
libjansson.