on bbd4189 help sign in
127 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 and response parsing and just return unformatted json data.
11Here's a simple thread that will:
13- grab JSON of the latest base-hash from https://www.whatsthelatestbasehash.com/latesthash.json
14- grab JSON of a list of all base-hashes with version info from https://www.whatsthelatestbasehash.com/all.json
15- format and cast them
16- check `all.json` includes the latest base-hash
17- print the details of the latest base-hash to the dojo
19#### `latest-hash.hoon`
21```hoon
22/- spider
23/+ *strandio
24=, strand=strand:spider
25=>
26|%
27++ url-1 "https://www.whatsthelatestbasehash.com/latesthash.json"
28++ url-2 "https://www.whatsthelatestbasehash.com/all.json"
29++ latest-format
30 =, dejs:format
31 %- ot
32 :~ ['hash' so]
33 ['timestamp' so]
34 ['last_five' so]
35 ==
36++ latest-sur
37 $: hash=@t
38 timestamp=@t
39 last-five=@t
40 ==
41++ all-format
42 =, dejs:format
43 %- ar
44 %- ot
45 :~ ['hash' so]
46 ['pill' bo]
47 ['version' so]
48 ['published' so]
49 ['release' so]
50 ==
51++ all-sur
52 $: hash=@t
53 pill=?
54 version=@t
55 published=@t
56 release=@t
57 ==
58--
59^- thread:spider
60|= arg=vase
61=/ m (strand ,vase)
62^- form:m
63;< js-latest=json bind:m (fetch-json url-1)
64;< js-all=json bind:m (fetch-json url-2)
65=/ latest `latest-sur`(latest-format js-latest)
66=/ all `(list all-sur)`(all-format js-all)
67=/ res (skim all |=(a=all-sur =(hash.a hash.latest)))
68?~ res
69 %- (slog leaf+"Not Found" ~) (pure:m !>(~))
70%- (slog leaf+"Hash: {(trip hash.i.res)}" ~)
71%- (slog leaf+"Pill? {?:(pill.i.res "Yes" "No")}" ~)
72%- (slog leaf+"Version: {(trip version.i.res)}" ~)
73%- (slog leaf+"Published: {(trip published.i.res)}" ~)
74%- (slog leaf+"Release: {(trip release.i.res)}" ~)
75(pure:m !>(~))
76```
78Save it as `/ted/latest-hash.hoon`, `|commit %home` and run it with `-latest-hash`. You should see something like:
80```
81> -latest-hash
82Hash: 0vj.dqqil.77m3k.a7v8d.mvrmi.8mavk.0a2nl.te6i6.cstfa.1ko6e.872gd
83Pill? No
84Version: v2.39
85Published: ~2021.3.10..02.20.48
86Release: https://github.com/urbit/urbit/releases/tag/urbit-os-v2.39
88```
90### Analysis
92`fetch-json` takes a url as a tape, so we've added a core with the urls like:
94```hoon
95=>
96|%
97++ url-1 "https://www.whatsthelatestbasehash.com/latesthash.json"
98++ url-2 "https://www.whatsthelatestbasehash.com/all.json"
99...
100```
102...then just called `fetch-json` like:
104```hoon
105;< js-latest=json bind:m (fetch-json url-1)
106```
108We've added formatting and structure arms like:
110```hoon
111++ latest-format
112 =, dejs:format
113 %- ot
114 :~ ['hash' so]
115 ['timestamp' so]
116 ['last_five' so]
117 ==
118++ latest-sur
119 $: hash=@t
120 timestamp=@t
121 last-five=@t
122 ==
123```
125...to turn it from `json` to a noun with faces. You can have a look at the JSON formatting functions under `dejs:format` in `zuse.hoon` for how to deal with the various json types.
127After that it's just some ordinary list logic and printing.