0

Emacs 26.1 Here json file:

[
  {
    "id": 11,
    "name": "my name"
  },
  {
    "id": 22,
    "name": "my name3"
  },
  {
    "id": 33,
    "name": "my name3"
  }
]

As you can see the count of elements are 3. Nice. Is it possible to get count of elements? Suppose I has large json and I need to get count of elements? Maybe has some method that can do this.

P.S. Structure of one item is:

{
    "id": xx,
    "name": "xxxx"
  }

2 Answers 2

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)))
0

Here my custom function:

(defun json-count-elements ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (message "Count of json elements: %d" (length (json-read)))))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.