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