Hot answers tagged

12

I've taken the liberty of adjusting your code appropriately: (require 'json) (let* ((json-object-type 'hash-table) (json-array-type 'list) (json-key-type 'string) (json (json-read-file "test.json"))) (car (gethash "projects" json))) The let* is required because otherwise the call to json-read-file will not see the previously bound ...


9

There are more folding options for Emacs: http://wikemacs.org/wiki/Folding#Yafolding and Yafolding looks like the best for your case since it is based on indentation. It also includes a nice interface à-la Magit to discover the commands. However, that doesn't fully answer your question since it won't show the range of the fields out of the box. Yafolding ...


6

You can instruct json-read-from-string to parse JSON arrays as elisp lists by let binding json-array-type to list like so (let ((json-object-type 'plist) (json-array-type 'list)) (setq mylist (json-read-from-string "[{\"name\": \"Adam\", \"id\": \"1\"}, {\"name\": \"Eve\", \"id\": \"2\"}]"))) ...


5

Here is a lightly adapted version of the export code you were using. The strategy I used to get selective export is to make a temporary org-file using org-map-entries and the match argument for it to only copy headings that match the criteria, and then to export the temp file. (require 'json) (defun org-export-json-buffer () (interactive) (let* ((tree (...


3

It's unlikely that there will be a solution here that will satisfy every user of the json package, since the problem is that elisp and json have differing semantics. In Lisp, the empty list and the lack of a value (nil) are the same thing. In Javascript (and therefore JSON), the empty array, the "empty" object, the boolean false value, and a lack of a value ...


3

add-hook does not take a mode symbol as its first parameter; it takes a symbol for a hook variable. Replace 'json-mode with 'json-mode-hook and you'll be all set. Note the differences between the symbols. One is a variable used to store a list of functions to run when entering json-mode: json-mode-hook is a variable defined in ‘json-mode.el’. ...


2

Use emacs' hideshow. You can keep your cursor anywhere & to hide blocks 'n' level below it, use the command C-c @ C-l Here, n is a numeric prefix argument that gets set by pressing C-u 1 RET Read more about hsmode here, hs-mode. More about prefix arguments here, prefix args hs mode doesn't work on Default mode. You need to have js2-mode (or) js-mode ...


2

So I realized my mistake shortly after submitting my question, and rather than deleting it I thought I'd answer in case someone else ends up here. Many of the keybindings on the Hide Show page are written in the form C-<something> @ C-<something> I took the @ to simply be a delimiter between the two chained commands, but it turns out it's ...


2

The following lisp code modifies org-babel-variable-assignments:plantuml. If you prefix a variable of a plantuml source block with json: newlines escaped with the character ?\\ are not stripped from the value of that variable. Instead the substring "\\\"" is reduced to "\"". If you want to use the Elisp snippet put it into your init file and restart Emacs. ...


2

Cute. Take a look at the beginning of the alist again: ((1 . [((id . 2) (attribute . salutation)… It looks like the key is a number, so I tried that (assoc 1 tester) nil It doesn't have any quotes, but maybe a string? (assoc "1" tester) nil Nope. It's actually a symbol whose name is the single digit 1: (assoc '\1 tester) (\1 . [((id . 2)… When I ...


2

Recursion for the win. (defun json-filter-paths (json paths) (mapcar (lambda (elt) (if (consp elt) (cons (car elt) (json-filter-paths (cdr (assoc (car elt) json)) (cdr elt))) (cons elt (cdr (assoc elt json))))) paths)) (json-filter-paths'((owner (login . "octocat") (avatar . "blah")...


2

Install json-mode might be the simplest. In my set up (I use straight.el) simply (use-package json-mode)


2

I probably don't understand your problem. Your question isn't very clear (what calls to message are you using?). But if I do understand what you're trying to do, it pretty much amounts to this: You have symbols whose names are numeric strings. E.g. the symbol 1 has name "1". (In Lisp, you can refer to such a symbol literally, using \1, but the ...


2

You can get the symbol name with symbol-name, which returns a string, and then convert the string to a number: (string-to-number (symbol-name '\1)) ==> 1


2

Org babel "Supported languages" are languages that you can evaluate/execute directly from an orgmode code block. I don't think JSON and XML are executable languages anywhere, so you don't need special support for them. You can still use src blocks, and if there's an Emacs mode for editing the language, it will apply the formatting to the code. If ...


2

This is a well-known long-standing bug in json.el that has been fixed in the latest development version of Emacs (what will be released in the future as Emacs 28): https://bugs.gnu.org/42545 If you don't have access to the development version of Emacs, but have an Emacs 27 built with native JSON support (i.e. configured --with-json to link against the ...


1

Here is a way to do this with literate programming in org. #+BEGIN_SRC json :tangle config.json {"key1": "value", #+END_SRC For key2 I prefer 0 instead of 1 #+BEGIN_SRC json :tangle config.json "key2": 0} #+END_SRC Here we automate tangling, and then show it is valid json. #+BEGIN_SRC emacs-lisp :var tangle=(org-babel-...


1

You can use json-read function json-read is a compiled Lisp function in json.el. Parse and return the JSON object following point. Advances point just past JSON object. like this (save-excursion (goto-char (point-min)) (length (json-read)))


1

Your configuration, (add-hook 'js-mode-hook 'json-pretty-print-buffer-ordered) means the function json-pretty-print-buffer-ordered will run anytime js-mode is started. This is probably not what you intend, as json-pretty-print-buffer-ordered will fail when the buffer it is called from doesn't contain a complete, correct JSON object. That's not necessarily ...


1

You can just make a list and then encode it as json like this: (require 'cl-lib) (require 'json) (json-encode (cl-loop for i to 100 collect (list (cons 'id i) (cons 'text "...")))) That gives me this string: [{"id":0,"text":"..."},{"id":1,"text":"..."},{"id":2,"text":"..."},{"id":3,"text":"..."},{"id":4,"text":"..."},{"id":5,"text":"..."},{"id":6,"text":"....


1

The json-navigator Emacs package displays any JSON document in a tree-like structure. It uses the hierarchy package.


1

You should try hs-hide-level with numeric prefix argument. The argument specifies the sub-level of the blocks to hide relative to the block you are in. If you want to have some kind of current level within the whole buffer and you want to hide the next or the previous sub-level you can try the following code. Paste that code into your init-file and re-...


1

I received the exact same error on a fresh Ubuntu install using Emacs 26.3. In my case it was related to the ob-ipython packet, which requires ipython and jupyter to be installed, which I hadn't come around to doing yet. After a sudo apt install ipython jupyter the error was gone.


1

For anyone who is curious, adding the following code in the init.el solved the issue. Turns out Rmacs was not being able to load JSON setup because it was looking in the wrong folder. I also had to install the package exec-path-from-shell. (when (memq window-system '(mac ns x)) (exec-path-from-shell-initialize))


1

C-x my-validate-json-or-js-expression validate the current buffer or selected region as json. The only requirement is you need install js2-mode. (No, you don't need setup js2-mode. My code works out of box! we use js2-mode as API collection instead of major-mode) Here is the code, (defun my-validate-json-or-js-expression (&optional not-json-p) "...


1

Emacs comes with hs-minor-mode which can be used selectively hide/show code and comment blocks in several languages, including JavaScript. By default this will let you hide and show JSON blocks delimited by '{' and '}' but it is easily modified to allow '[' and ']' as block delimeters as well. To do this we need to modify the js-mode entry in hs-special-...


1

I have the same problem. Turns out I'm using js2-mode as minor-mode for js-mode (add-hook 'js-mode-hook 'js2-minor-mode) Everything is ok after remove above code from my .emacs.el Another option is to add js2-minor-mode only if it's not json-mode (add-hook 'js-mode-hook '(lambda () (unless (eq major-mode 'json-mode) (js2-minor-...


1

You can re-add /usr/share/emacs/24.5/lisp/ to the start of the load-path by doing something like this (push "/usr/share/emacs/24.5/lisp" load-path) While this will work, this is system/installation dependent, I found one installation independent way to get path to lisp startup.el (expand-file-name "../lisp" data-directory) Combining these together the ...


Only top voted, non community-wiki answers of a minimum length are eligible