transform.Unmarshal.md (2210B)
1 --- 2 title: "transform.Unmarshal" 3 description: "`transform.Unmarshal` (alias `unmarshal`) parses the input and converts it into a map or an array. Supported formats are JSON, TOML, YAML, XML and CSV." 4 date: 2018-12-23 5 categories: [functions] 6 menu: 7 docs: 8 parent: "functions" 9 keywords: [] 10 signature: ["RESOURCE or STRING | transform.Unmarshal [OPTIONS]"] 11 hugoversion: "0.53" 12 aliases: [] 13 --- 14 15 The function accepts either a `Resource` created in [Hugo Pipes](/hugo-pipes/) or via [Page Bundles](/content-management/page-bundles/), or simply a string. The two examples below will produce the same map: 16 17 ```go-html-template 18 {{ $greetings := "hello = \"Hello Hugo\"" | transform.Unmarshal }}` 19 ``` 20 21 ```go-html-template 22 {{ $greetings := "hello = \"Hello Hugo\"" | resources.FromString "data/greetings.toml" | transform.Unmarshal }} 23 ``` 24 25 In both the above examples, you get a map you can work with: 26 27 ```go-html-template 28 {{ $greetings.hello }} 29 ``` 30 31 The above prints `Hello Hugo`. 32 33 ## CSV Options 34 35 Unmarshal with CSV as input has some options you can set: 36 37 delimiter 38 : The delimiter used, default is `,`. 39 40 comment 41 : The comment character used in the CSV. If set, lines beginning with the comment character without preceding whitespace are ignored.: 42 43 Example: 44 45 ```go-html-template 46 {{ $csv := "a;b;c" | transform.Unmarshal (dict "delimiter" ";") }} 47 ``` 48 49 ## XML data 50 51 As a convenience, Hugo allows you to access XML data in the same way that you access JSON, TOML, and YAML: you do not need to specify the root node when accessing the data. 52 53 To get the contents of `<title>` in the document below, you use `{{ .message.title }}`: 54 55 ``` 56 <root> 57 <message> 58 <title>Hugo rocks!</title> 59 <description>Thanks for using Hugo</description> 60 </message> 61 </root> 62 ``` 63 64 The following example lists the items of an RSS feed: 65 66 ``` 67 {{ with resources.Get "https://example.com/rss.xml" | transform.Unmarshal }} 68 {{ range .channel.item }} 69 <strong>{{ .title | plainify | htmlUnescape }}</strong><br /> 70 <p>{{ .description | plainify | htmlUnescape }}</p> 71 {{ $link := .link | plainify | htmlUnescape }} 72 <a href="{{ $link }}">{{ $link }}</a><br /> 73 <hr> 74 {{ end }} 75 {{ end }} 76 ```