2023-10-20 03:42:39 -04:00
---
title: data.GetJSON
description: Returns a JSON object from a local or remote JSON file, or an error if the file does not exist.
2023-12-04 09:14:18 -05:00
categories: []
2023-10-20 03:42:39 -04:00
keywords: []
2023-12-04 09:14:18 -05:00
action:
2023-10-20 03:42:39 -04:00
aliases: [getJSON]
2023-12-04 09:14:18 -05:00
related:
- functions/data/GetCSV
- functions/resources/Get
- functions/resources/GetRemote
- methods/page/Resources
2023-10-20 03:42:39 -04:00
returnType: any
2023-12-04 09:14:18 -05:00
signatures: ['data.GetJSON INPUT... [OPTIONS]']
2023-10-20 03:42:39 -04:00
toc: true
---
Given the following directory structure:
```text
my-project/
└── other-files/
└── books.json
```
Access the data with either of the following:
```go-html-template
2023-12-04 09:14:18 -05:00
{{ $data := getJSON "other-files/books.json" }}
{{ $data := getJSON "other-files/" "books.json" }}
2023-10-20 03:42:39 -04:00
```
2023-12-04 09:14:18 -05:00
{{% note %}}
When working with local data, the filepath is relative to the working directory.
{{% /note %}}
2023-10-20 03:42:39 -04:00
Access remote data with either of the following:
```go-html-template
2023-12-04 09:14:18 -05:00
{{ $data := getJSON "https://example.org/books.json" }}
{{ $data := getJSON "https://example.org/" "books.json" }}
2023-10-20 03:42:39 -04:00
```
The resulting data structure is a JSON object:
```json
[
{
"author": "Victor Hugo",
"rating": 5,
"title": "Les Misérables"
},
{
"author": "Victor Hugo",
"rating": 4,
"title": "The Hunchback of Notre Dame"
}
]
```
2023-12-04 09:14:18 -05:00
## Options
Add headers to the request by providing an options map:
```go-html-template
{{ $opts := dict "Authorization" "Bearer abcd" }}
{{ $data := getJSON "https://example.org/books.json" $opts }}
```
Add multiple headers using a slice:
```go-html-template
{{ $opts := dict "X-List" (slice "a" "b" "c") }}
{{ $data := getJSON "https://example.org/books.json" $opts }}
```
2023-10-20 03:42:39 -04:00
## Global resource alternative
2023-12-04 09:14:18 -05:00
Consider using the [`resources.Get`] function with [`transform.Unmarshal`] when accessing a global resource.
2023-10-20 03:42:39 -04:00
```text
my-project/
└── assets/
└── data/
└── books.json
```
```go-html-template
{{ $data := "" }}
{{ $p := "data/books.json" }}
{{ with resources.Get $p }}
2024-01-26 02:11:52 -05:00
{{ $data = . | transform.Unmarshal }}
2023-10-20 03:42:39 -04:00
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
```
## Page resource alternative
2023-12-04 09:14:18 -05:00
Consider using the [`Resources.Get`] method with [`transform.Unmarshal`] when accessing a page resource.
2023-10-20 03:42:39 -04:00
```text
my-project/
└── content/
└── posts/
└── reading-list/
├── books.json
└── index.md
```
```go-html-template
{{ $data := "" }}
{{ $p := "books.json" }}
{{ with .Resources.Get $p }}
2024-01-26 02:11:52 -05:00
{{ $data = . | transform.Unmarshal }}
2023-10-20 03:42:39 -04:00
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
```
## Remote resource alternative
2023-12-04 09:14:18 -05:00
Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`] when accessing a remote resource to improve error handling and cache control.
2023-10-20 03:42:39 -04:00
```go-html-template
{{ $data := "" }}
{{ $u := "https://example.org/books.json" }}
{{ with resources.GetRemote $u }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
2024-01-26 02:11:52 -05:00
{{ $data = . | transform.Unmarshal }}
2023-10-20 03:42:39 -04:00
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $u }}
{{ end }}
```
2023-12-04 09:14:18 -05:00
[`Resources.Get`]: methods/page/Resources
[`resources.GetRemote`]: /functions/resources/getremote
[`resources.Get`]: /functions/resources/get
2023-10-20 03:42:39 -04:00
[`transform.Unmarshal`]: /functions/transform/unmarshal