Skip to main content
add another usage example
Source Link
wasamasa
  • 22.6k
  • 1
  • 73
  • 101

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 values as let sets them in parallel...

As for the access functions, gethash retrieves a value by key from a hash table, whereas car returns the first list element. If you need a different one, you can use nth with an index.

To actually iterate over the projects:

(require 'json)

(let* ((json-object-type 'hash-table)
       (json-array-type 'list)
       (json-key-type 'string)
       (json (json-read-file "test.json"))
       (projects (gethash "projects" json)))
  (dolist (project projects)
    ...))

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 values as let sets them in parallel...

As for the access functions, gethash retrieves a value by key from a hash table, whereas car returns the first list element. If you need a different one, you can use nth with an index.

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 values as let sets them in parallel...

As for the access functions, gethash retrieves a value by key from a hash table, whereas car returns the first list element. If you need a different one, you can use nth with an index.

To actually iterate over the projects:

(require 'json)

(let* ((json-object-type 'hash-table)
       (json-array-type 'list)
       (json-key-type 'string)
       (json (json-read-file "test.json"))
       (projects (gethash "projects" json)))
  (dolist (project projects)
    ...))
Source Link
wasamasa
  • 22.6k
  • 1
  • 73
  • 101

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 values as let sets them in parallel...

As for the access functions, gethash retrieves a value by key from a hash table, whereas car returns the first list element. If you need a different one, you can use nth with an index.