fcc3ed651Remove some expired new-ina9c5981f5Fix cascade example82bb250faAdd some lines about permalinks tokens in front matter328fe564eRemove some outdated new-infb140b153Hide showcase menu entry42d9d1c79Update image formats from which EXIF data can be extracted09ad56b6enetlify: Hugo 0.130.01d503f846Merge branch 'tempv0.130.0'e2458074dmath: Add trigonometric functions and some angle helper functions392afc8f9Disable the showcase section for now0300750f2Improve example of image render hook60a9306afImprove description of the .Site.RegularPages method8d759175dFix typos55daa4554Update XxHash.md397c81cb7Add namespace for hash functions70fe8d2f0netlify: Bump Hugo 0.129.05a9771affMerge branch 'tempv0.129.0'f9146575bFix typoe6e1fea49Fix typo in Hugo docs | functions | partial732d10ec4source: Expose GitInfo Body34c97e639netlify: Hugo 0.128.23270587e9Fix typo727c5396enetlify: Hugo 0.128.180b6ae99cUpdate GitHub Pages workflow file example027134102Update GitHub Pages workflow file example2600a8a2eMiscellaneous edits3fdd5819bUpdate Build.md7764005c3Improve example of render hook directory structure5e3941d82Fix typos748bf065fRestructure templates sectionfafbf6566Update Defer.md012162e0dDocument changes to template functions in v0.128.00990ce35bquick-reference: Update emojis6677a30efUpdate Goldmark configuration documentation4449d530dDocument new pagination config0af8be439Update Defer.md56348196dnetlify: Hugo 0.128.0d67b6d82eUpdate content/en/functions/templates/Defer.md23d996b3dUpdate content/en/functions/templates/Defer.md7f7fb2f27Document templates.Defer5ada1e9d5Fix docs merge (remove shortcode)d27ee6156Merge branch 'tempv0.128.0'5d7317c84Fix typo7c18ee546Update theme83bfea63bUpdate themeb274b3238Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722'ff34a035adeploy: Add stripIndexHtml target optiond9e964bdbmarkup/goldmark: Add the Hugo Goldmark Extras "delete" extensionac5bd16d2deps: Upgrade github.com/alecthomas/chroma v2.13.0 => v2.14.025377171bconfig: Remove extraneous BuildConfig setting0d2044f6ddocs: Regen docshelpera2548dac9markup/goldmark: Support extras extension9d0c86ee8commands: Add gen chromastyles --lineNumbersTableStyle flag git-subtree-dir: docs git-subtree-split:fcc3ed651a
3.7 KiB
title, description, categories, keywords, action, toc, expiryDate
| title | description | categories | keywords | action | toc | expiryDate | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| data.GetJSON | Returns a JSON object from a local or remote JSON file, or an error if the file does not exist. |
|
true | 2025-02-19 |
{{% deprecated-in 0.123.0 %}}
Instead, use transform.Unmarshal with a global, page, or remote resource.
See the remote data example.
{{% /deprecated-in %}}
Given the following directory structure:
my-project/
└── other-files/
└── books.json
Access the data with either of the following:
{{ $data := getJSON "other-files/books.json" }}
{{ $data := getJSON "other-files/" "books.json" }}
{{% note %}} When working with local data, the file path is relative to the working directory. {{% /note %}}
Access remote data with either of the following:
{{ $data := getJSON "https://example.org/books.json" }}
{{ $data := getJSON "https://example.org/" "books.json" }}
The resulting data structure is a JSON object:
[
{
"author": "Victor Hugo",
"rating": 5,
"title": "Les Misérables"
},
{
"author": "Victor Hugo",
"rating": 4,
"title": "The Hunchback of Notre Dame"
}
]
Options
Add headers to the request by providing an options map:
{{ $opts := dict "Authorization" "Bearer abcd" }}
{{ $data := getJSON "https://example.org/books.json" $opts }}
Add multiple headers using a slice:
{{ $opts := dict "X-List" (slice "a" "b" "c") }}
{{ $data := getJSON "https://example.org/books.json" $opts }}
Global resource alternative
Consider using the resources.Get function with transform.Unmarshal when accessing a global resource.
my-project/
└── assets/
└── data/
└── books.json
{{ $data := dict }}
{{ $p := "data/books.json" }}
{{ with resources.Get $p }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
Page resource alternative
Consider using the Resources.Get method with transform.Unmarshal when accessing a page resource.
my-project/
└── content/
└── posts/
└── reading-list/
├── books.json
└── index.md
{{ $data := dict }}
{{ $p := "books.json" }}
{{ with .Resources.Get $p }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
Remote resource alternative
Consider using the resources.GetRemote function with transform.Unmarshal when accessing a remote resource to improve error handling and cache control.
{{ $data := dict }}
{{ $u := "https://example.org/books.json" }}
{{ with resources.GetRemote $u }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $u }}
{{ end }}