I have a database that returns data as a tree like this:
'((7 "vince" "[email protected]" "space" "no value" 1)
(8 "vince" "[email protected]" "place" "no value" 1)
(9 "Smith" "[email protected]" "now" "no value" 1))
The second column is first name and the third column is email.
My goal is to return JSON key value pairs but im struggling
Here is what I have tried:
- Function to get name and email from one list item
(defun get-name-&-emails-db1 (lst)
(if (null lst)
nil
(let* ((name (second lst))
(email (third lst)))
(cl-json:encode-json-to-string `((:name . ,name)(:email . ,email))))))
- Map over data set
(mapcar #'get-name-&-emails-db1 (return-data-tree))
This returns a list of individual json blocks. But I want it to be ONE json block with all records.
What am I missing?
(ideally, I want to know how to do this without any additional libraries)
Thanks
4
Answers
I tried to encode a list of alists, and this is how it goes:
If this is what you want to have, then you need to organize your data in Lisp first, then encode the whole list as JSON instead of formatting each entry individually.
Use
mapcar
, get the second and third element of each entry, and then callcl-json:encode-json-to-string
on the result:Here I don’t use comma, backquote, alists or plists, but simply: I create a list of hash-tables. I’m quite sure how a list and a hash table are rendered in JSON, so let’s rework our data a bit to come back in known territories.
I like Serapeum’s
dict
:Answers were given. Just a general way to deal with alist and json: