Created
June 15, 2023 20:39
-
-
Save bahmanm/8f036451ca661f7f3f3f6588b6a4c15f to your computer and use it in GitHub Desktop.
eLisp: Save and parse JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; to save an object into a json file | |
(with-temp-buffer | |
(json-insert '(:foo "bar")) | |
(write-file "~/tmp/my.json")) | |
;; load a json file as hash table | |
(with-temp-buffer | |
(insert-file-contents "~/tmp/my.json") | |
(json-parse-buffer)) |
Each time you call json-insert
a new JSON object is appended to the contents of the temp buffer. That is after the first section of your code the file looks like ⬇️ which is invalid json.
{"foo": "bar"}
{"test": "hallo"}
The idea is to call json-insert
only once with the final configs object to be saved. For example:
(with-temp-buffer
(let ((configs-to-be-saved '(:foo "bar")))
(plist-put configs-to-be-saved
:baz
'(:x 10 :y "lorem"))
(json-insert configs-to-be-saved)
(write-file "~/tmp/my.json")))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. I haven't been working a lot with JSON before. Why does this not work (i.e. the output is "nil"):