How to parse JSON in Racket?

I canโ€™t understand that the { documentation }, in fact, there are no examples of parsing some simple JSON data, so I was wondering if anyone here could give some examples to start with.

+6
source share
1 answer

Here is a very simple example:

(require json) (define x (string->jsexpr "{\"foo\": \"bar\", \"bar\": \"baz\"}")) (for (((key val) (in-hash x))) (printf "~a = ~a~%" key val)) 

Here you can use it with a JSON based API:

 (require net/http-client json) (define-values (status header response) (http-sendrecv "httpbin.org" "/ip" #:ssl? 'tls)) (define data (read-json response)) (printf "My IP address is ~a~%" (hash-ref data 'origin)) 

In the OP request here, you can create a JSON value from a structure type:

 (require json) (struct person (first-name last-name age country)) (define (person->jsexpr p) (hasheq 'first-name (person-first-name p) 'last-name (person-last-name p) 'age (person-age p) 'country (person-country p))) (define cky (person "Chris" "Jester-Young" 33 "New Zealand")) (jsexpr->string (person->jsexpr cky)) 
+16
source

Source: https://habr.com/ru/post/969547/


All Articles