Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elisp - json-read-from-string on response from an API

Tags:

json

emacs

elisp

I'm using json.el, and furl.el to get time entries from toggl.com's API - Unfortunately I don't think it's seperating the response into multiple lists:

JSON Response (Two entries in data):

{"data":[{"id":50439783,"description":"bar","billable":false,"start":"2012-10-20T18:07:00Z","stop":"2012-10-20T22:39:00Z","duration":16320,"workspace":{"id":172049,"name":"Dan's workspace"},"tag_names":[],"ignore_start_and_stop":false,"updated_at":"2012-10-20T22:40:26Z","user_id":231062},{"id":50440682,"description":"Test Time Entry","billable":false,"start":"2012-10-20T23:40:16Z","stop":"2012-10-20T23:50:16Z","duration":600,"workspace":{"id":172049,"name":"Dan's workspace"},"tag_names":[],"ignore_start_and_stop":false,"updated_at":"2012-10-20T23:51:32Z","user_id":231062}],"related_data_updated_at":"2012-10-19T00:00:00Z"}

json-read-from-string response:

(related_data_updated_at . 2012-10-19T00:00:00Z)                                                                                                                          
(data . [((user_id . 231062) (updated_at . 2012-10-20T22:40:26Z) (ignore_start_and_stop . :json-false) (tag_names . []) (workspace ... ...) (duration . 16320) (stop . 20\
12-10-20T22:39:00Z) (start . 2012-10-20T18:07:00Z) (billable . :json-false) (description . bar) (id . 50439783)) ((user_id . 231062) (updated_at . 2012-10-20T23:51:32Z) \
(ignore_start_and_stop . :json-false) (tag_names . []) (workspace ... ...) (duration . 600) (stop . 2012-10-20T23:50:16Z) (start . 2012-10-20T23:40:16Z) (billable . :jso\
n-false) (description . Test Time Entry) (id . 50440682))])

So if that list is considered multiple entries, how could I access the first? car displays related_data_updated_at, and the key data. cdr displays the val of related_data_updated_at, and everything else.

I don't think the elisp will help much, but here it is just incase:

(defun toggl-display-time-entries(json-string)                                                                                                                            
  (with-current-buffer (get-buffer-create "Toggl")                                                                                                                        
    (erase-buffer)                                                                                                                                                        
    (let ((inhibit-read-only t)                                                                                                                                           
          (time-entries (json-read-from-string json-string)))                                                                                                             
      (dolist (time-entry time-entries)                                                                                                                                   
        (newline)                                                                                                                                                         
        (insert (format "%s" (cdr time-entry)))))))
like image 330
Dan LaManna Avatar asked Oct 21 '12 02:10

Dan LaManna


1 Answers

You're close but you need to extract the 'data and then iterate over the array, not a list. For example this prints the user id from each item in the array:

(let ((array-of-times
       (cdr 
    (assoc 'data
           (json-read-from-string json-string)))))
  (let ((c (length array-of-times)))
    (dotimes (n c)
      (insert (format "user_id %s\n" (cdr (assoc 'user_id (elt array-of-times n))))))))
like image 53
justinhj Avatar answered Nov 11 '22 06:11

justinhj