summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions/data/GetJSON.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/content/en/functions/data/GetJSON.md')
-rw-r--r--docs/content/en/functions/data/GetJSON.md55
1 files changed, 38 insertions, 17 deletions
diff --git a/docs/content/en/functions/data/GetJSON.md b/docs/content/en/functions/data/GetJSON.md
index 37ee8e9a1..96812e7c0 100644
--- a/docs/content/en/functions/data/GetJSON.md
+++ b/docs/content/en/functions/data/GetJSON.md
@@ -1,19 +1,17 @@
---
title: data.GetJSON
-linkTitle: getJSON
description: Returns a JSON object from a local or remote JSON file, or an error if the file does not exist.
-categories: [functions]
+categories: []
keywords: []
-menu:
- docs:
- parent: functions
-function:
+action:
aliases: [getJSON]
+ related:
+ - functions/data/GetCSV
+ - functions/resources/Get
+ - functions/resources/GetRemote
+ - methods/page/Resources
returnType: any
- signatures: [data.GetJSON PATHPART...]
-relatedFunctions:
- - data.GetCSV
- - data.GetJSON
+ signatures: ['data.GetJSON INPUT... [OPTIONS]']
toc: true
---
@@ -28,15 +26,19 @@ my-project/
Access the data with either of the following:
```go-html-template
-{{ $data := getCSV "," "other-files/books.json" }}
-{{ $data := getCSV "," "other-files/" "books.json" }}
+{{ $data := getJSON "other-files/books.json" }}
+{{ $data := getJSON "other-files/" "books.json" }}
```
+{{% note %}}
+When working with local data, the filepath is relative to the working directory.
+{{% /note %}}
+
Access remote data with either of the following:
```go-html-template
-{{ $data := getCSV "," "https://example.org/books.json" }}
-{{ $data := getCSV "," "https://example.org/" "books.json" }}
+{{ $data := getJSON "https://example.org/books.json" }}
+{{ $data := getJSON "https://example.org/" "books.json" }}
```
The resulting data structure is a JSON object:
@@ -56,9 +58,25 @@ The resulting data structure is a JSON object:
]
```
+## 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 }}
+```
+
## Global resource alternative
-Consider using `resources.Get` with [`transform.Unmarshal`] when accessing a global resource.
+Consider using the [`resources.Get`] function with [`transform.Unmarshal`] when accessing a global resource.
```text
my-project/
@@ -80,7 +98,7 @@ my-project/
## Page resource alternative
-Consider using `.Resources.Get` with [`transform.Unmarshal`] when accessing a page resource.
+Consider using the [`Resources.Get`] method with [`transform.Unmarshal`] when accessing a page resource.
```text
my-project/
@@ -104,7 +122,7 @@ my-project/
## Remote resource alternative
-Consider using `resources.GetRemote` with [`transform.Unmarshal`] for improved error handling when accessing a remote resource.
+Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`] when accessing a remote resource to improve error handling and cache control.
```go-html-template
{{ $data := "" }}
@@ -121,4 +139,7 @@ Consider using `resources.GetRemote` with [`transform.Unmarshal`] for improved e
{{ end }}
```
+[`Resources.Get`]: methods/page/Resources
+[`resources.GetRemote`]: /functions/resources/getremote
+[`resources.Get`]: /functions/resources/get
[`transform.Unmarshal`]: /functions/transform/unmarshal