Merge commit '35dec7c96f7ee3eb17dd444f7067f0c776fb56ae'

This commit is contained in:
Bjørn Erik Pedersen
2023-12-04 15:24:01 +01:00
810 changed files with 24147 additions and 7766 deletions

View File

@@ -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