Please help with json. I am trying to create a json object like so … (require json)
(define a "hello")
(write-json #hasheq((key1 . a)))
;; expected result
{"key1": "hello"}
;; actual result ..
{"key1":
; write-json: expected argument of type <legal JSON value>; given: 'a
; Context (plain; to see better errortrace context, re-run with C-u prefix):
; /usr/share/racket/collects/json/main.rkt:132:2 loop
What am I doing wrong?
Try
(write-json #hasheq((key1 . "a")))
Oh, you want to make it "hello"
You can’t use #hasheq
then. It constructs the hash literally
I wanted "hello"
.
Instead, use:
(write-json (hash 'key1 a))
Ah…so I can’t use #hasheq
.. OK!
This is like '(a)
vs (list a)
They are different. See https://stackoverflow.com/questions/34984552/what-is-the-difference-between-quote-and-list for more info
Thanks. I will review the article.
How to generated this json object .. {"key1": “hello”, “key2”, “goodbye”} ?
Can I do this …
(write-json (hash ’key1 a ’key2 b)) ??
Yes, assuming that a
is bound to "hello"
and b
is bound to "goodbye"
yup…that works! thanks!
@mflatt This still fails. I entered a ticket: https://github.com/racket/racket-pkg-website/issues/76
Also `#hasheq((key . ,a))
(The unquote only works for the values, not they keys)
@jorge.garcia.gonzalez has joined the channel