GetJSON.md - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
GetJSON.md (3550B)
---
1 ---
2 title: data.GetJSON
3 description: Returns a JSON object from a local or remote JSON file, or an error if the file does not exist.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [getJSON]
9 returnType: any
10 signatures: ['data.GetJSON INPUT... [OPTIONS]']
11 expiryDate: 2026-02-19 # deprecated 2024-02-19 in v0.123.0
12 ---
13
14 {{< deprecated-in 0.123.0 >}}
15 Instead, use [`transform.Unmarshal`] with a [global resource](g), [page resource](g), or [remote resource](g).
16
17 See the [remote data example].
18
19 [`transform.Unmarshal`]: /functions/transform/unmarshal/
20 [remote data example]: /functions/resources/getremote/#remote-data
21 {{< /deprecated-in >}}
22
23 Given the following directory structure:
24
25 ```text
26 my-project/
27 └── other-files/
28 └── books.json
29 ```
30
31 Access the data with either of the following:
32
33 ```go-html-template
34 {{ $data := getJSON "other-files/books.json" }}
35 {{ $data := getJSON "other-files/" "books.json" }}
36 ```
37
38 > [!note]
39 > When working with local data, the file path is relative to the working directory.
40
41 Access remote data with either of the following:
42
43 ```go-html-template
44 {{ $data := getJSON "https://example.org/books.json" }}
45 {{ $data := getJSON "https://example.org/" "books.json" }}
46 ```
47
48 The resulting data structure is a JSON object:
49
50 ```json
51 [
52 {
53 "author": "Victor Hugo",
54 "rating": 5,
55 "title": "Les Misérables"
56 },
57 {
58 "author": "Victor Hugo",
59 "rating": 4,
60 "title": "The Hunchback of Notre Dame"
61 }
62 ]
63 ```
64
65 ## Options
66
67 Add headers to the request by providing an options map:
68
69 ```go-html-template
70 {{ $opts := dict "Authorization" "Bearer abcd" }}
71 {{ $data := getJSON "https://example.org/books.json" $opts }}
72 ```
73
74 Add multiple headers using a slice:
75
76 ```go-html-template
77 {{ $opts := dict "X-List" (slice "a" "b" "c") }}
78 {{ $data := getJSON "https://example.org/books.json" $opts }}
79 ```
80
81 ## Global resource alternative
82
83 Consider using the [`resources.Get`] function with [`transform.Unmarshal`] when accessing a global resource.
84
85 ```text
86 my-project/
87 └── assets/
88 └── data/
89 └── books.json
90 ```
91
92 ```go-html-template
93 {{ $data := dict }}
94 {{ $p := "data/books.json" }}
95 {{ with resources.Get $p }}
96 {{ $data = . | transform.Unmarshal }}
97 {{ else }}
98 {{ errorf "Unable to get resource %q" $p }}
99 {{ end }}
100 ```
101
102 ## Page resource alternative
103
104 Consider using the [`Resources.Get`] method with [`transform.Unmarshal`] when accessing a page resource.
105
106 ```text
107 my-project/
108 └── content/
109 └── posts/
110 └── reading-list/
111 ├── books.json
112 └── index.md
113 ```
114
115 ```go-html-template
116 {{ $data := dict }}
117 {{ $p := "books.json" }}
118 {{ with .Resources.Get $p }}
119 {{ $data = . | transform.Unmarshal }}
120 {{ else }}
121 {{ errorf "Unable to get resource %q" $p }}
122 {{ end }}
123 ```
124
125 ## Remote resource alternative
126
127 Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`] when accessing a remote resource to improve error handling and cache control.
128
129 ```go-html-template
130 {{ $data := dict }}
131 {{ $url := "https://example.org/books.json" }}
132 {{ with try (resources.GetRemote $url) }}
133 {{ with .Err }}
134 {{ errorf "%s" . }}
135 {{ else with .Value }}
136 {{ $data = . | transform.Unmarshal }}
137 {{ else }}
138 {{ errorf "Unable to get remote resource %q" $url }}
139 {{ end }}
140 {{ end }}
141 ```
142
143 [`Resources.Get`]: /methods/page/resources/
144 [`resources.GetRemote`]: /functions/resources/getremote/
145 [`resources.Get`]: /functions/resources/get/
146 [`transform.Unmarshal`]: /functions/transform/unmarshal/