> For the complete documentation index, see [llms.txt](https://docs.urbit.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.urbit.org/urbit-os/base/threads/examples/get-json.md).

# Fetch JSON

Grabbing JSON from some url is very easy.

`strandio` includes the `+fetch-json` function which will handle the HTTP request, response, and parsing, producing JSON.

The following thread fetches the current Bitcoin price from the [CoinGecko API](https://www.coingecko.com/en/api) in the specified currency and prints it to the terminal.

{% code title="/ted/btc-price.hoon" lineNumbers="true" %}

```hoon
/+  *strandio
=,  dejs-soft:format
|=  arg=vase
=/  m  (strand:rand ,vase)
^-  form:m
=/  url
  "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies="
=/  cur  !<((unit @tas) arg)
?~  cur  (strand-fail:rand %no-arg ~)
=.  u.cur  (crip (cass (trip u.cur)))
?.  ((sane %tas) u.cur)  (strand-fail:rand %bad-currency-format ~)
;<  =json  bind:m  (fetch-json (weld url (trip u.cur)))
=/  price=(unit @ta)  ((ot ~[bitcoin+(ot [u.cur no]~)]) json)
?~  price  ((slog 'Currency not found.' ~) (pure:m !>(~)))
%-  (slog leaf+"{(trip u.price)} {(cuss (trip u.cur))}" ~)
(pure:m !>(~))
```

{% endcode %}

Save 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:

```
> -btc-price %usd
49168 USD
```

You can try with other currencies as well:

```
> -btc-price %nzd
72455 NZD
> -btc-price %aud
68866 AUD
> -btc-price %gbp
37319 GBP
```

### Analysis <a href="#analysis" id="analysis"></a>

The thread takes a `@tas` as its argument, which the dojo wraps in a `$unit`. We extract the `$vase` and check it's not empty:

```hoon
=/  cur  !<((unit @tas) arg)
?~  cur  (strand-fail:rand %no-arg ~)
```

We then convert it to lowercase and check it's a valid `@tas`:

```hoon
=.  u.cur  (crip (cass (trip u.cur)))
?.  ((sane %tas) u.cur)  (strand-fail:rand %bad-currency-format ~)
```

Next, we use the `+fetch-json` function in `strandio` like so:

```hoon
;<  =json  bind:m  (fetch-json (weld url (trip u.cur)))
```

We convert the currency to a `$tape` and `+weld` it to the end of the `url`, which we give as an argument to `+fetch-json`. The `+fetch-json` function will make the request to the URL, receive the result, parse the JSON and produce the result as a `$json` structure.

The JSON the API produces looks like:

```json
{
  "bitcoin": {
    "usd": 49477
  }
}
```

Since it's an object in an object, we decode them using nested [`ot:dejs-soft:format`](/hoon/zuse/2d_7.md#otdejs-softformat) functions, and the price itself using [`no:dejs-soft:format`](/hoon/zuse/2d_7.md#nodejs-softformat) to produce a `(unit @ta)`:

```hoon
=/  price=(unit @ta)  ((ot ~[bitcoin+(ot [u.cur no]~)]) json)
```

Finally, 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 `~`):

```hoon
?~  price  ((slog 'Currency not found.' ~) (pure:m !>(~)))
%-  (slog leaf+"{(trip u.price)} {(cuss (trip u.cur))}" ~)
(pure:m !>(~))
```

For more information about working with `$json`, see the [JSON Guide](/hoon/json-guide.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.urbit.org/urbit-os/base/threads/examples/get-json.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
