on 514b5fc help sign in
114 lines 3.1 kB history raw
1+++
2title = "Fetch JSON"
3weight = 1
4template = "doc.html"
5+++
7Grabbing JSON from some url is very easy.
9`strandio` includes the `fetch-json` function which will handle the HTTP request, response, and parsing, producing `json`.
11The following thread fetches the current Bitcoin price from the [CoinGecko
12API](https://www.coingecko.com/en/api) in the specified currency and prints it
13to the terminal.
15#### `btc-price.hoon`
17```hoon
18/- spider
19/+ *strandio
20=, strand=strand:spider
21=, dejs-soft:format
22=, strand-fail=strand-fail:libstrand:spider
23^- thread:spider
24|= arg=vase
25=/ m (strand ,vase)
26^- form:m
27=/ url
28 "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies="
29=/ cur !<((unit @tas) arg)
30?~ cur (strand-fail %no-arg ~)
31=. u.cur (crip (cass (trip u.cur)))
32?. ((sane %tas) u.cur) (strand-fail %bad-currency-format ~)
33;< =json bind:m (fetch-json (weld url (trip u.cur)))
34=/ price=(unit @ta) ((ot ~[bitcoin+(ot [u.cur no]~)]) json)
35?~ price ((slog 'Currency not found.' ~) (pure:m !>(~)))
36%- (slog leaf+"{(trip u.price)} {(cuss (trip u.cur))}" ~)
37(pure:m !>(~))
38```
40Save it as `/ted/btc-price.hoon` in the `%base` desk of a fake ship, `|commit %base` and run it with `-btc-price %usd`. You should see something like:
42```
43> -btc-price %usd
4449168 USD
45```
47You can try with other currencies as well:
49```
50> -btc-price %nzd
5172455 NZD
52> -btc-price %aud
5368866 AUD
54> -btc-price %gbp
5537319 GBP
56```
58### Analysis
60The thread takes an `@tas` as its argument, which the dojo wraps in a `unit`. We extract the `vase` and check it's not empty:
62```hoon
63=/ cur !<((unit @tas) arg)
64?~ cur (strand-fail %no-arg ~)
65```
67We then convert it to lowercase and check it's a valid `@tas`:
69```hoon
70=. u.cur (crip (cass (trip u.cur)))
71?. ((sane %tas) u.cur) (strand-fail %bad-currency-format ~)
72```
74Next, we use the `fetch-json` function in `strandio` like so:
76```hoon
77;< =json bind:m (fetch-json (weld url (trip u.cur)))
78```
80We convert the currency to a `tape` and `weld` it to the end of the `url`, which
81we give as an argument to `fetch-json`. The `fetch-json` function will make the
82request to the URL, receive the result, parse the JSON and produce the result as
83a `json` structure.
85The JSON the API produces looks like:
87```json
89 "bitcoin": {
90 "usd": 49477
91 }
93```
95Since it's an object in an object, we decode them using nested
96[`ot:dejs-soft:format`](/docs/hoon/reference/zuse/2d_7#otdejs-softformat)
97functions, and the price itself using
98[`no:dejs-soft:format`](/docs/hoon/reference/zuse/2d_7#nodejs-softformat) to
99produce a `(unit @ta)`:
101```hoon
102=/ price=(unit @ta) ((ot ~[bitcoin+(ot [u.cur no]~)]) json)
103```
105Finally, we check if the `unit` is null and either print an error or print the price to the terminal with `slog` functions (the thread itself produces `~`):
107```hoon
108?~ price ((slog 'Currency not found.' ~) (pure:m !>(~)))
109%- (slog leaf+"{(trip u.price)} {(cuss (trip u.cur))}" ~)
110(pure:m !>(~))
111```
113For more information about working with `json`, see the [JSON
114Guide](/docs/hoon/guides/json-guide).