1--- 2title: data functions 3menu: 4 main: 5 parent: functions 6--- 7 8A collection of functions that retrieve, parse, and convert structured data. 9 10## `datasource` 11 12**Alias:** `ds` 13 14Parses a given datasource (provided by the [`--datasource/-d`](../../usage/#datasource-d) argument or [`defineDatasource`](#definedatasource)). 15 16If the `alias` is undefined, but is a valid URL, `datasource` will dynamically read from that URL. 17 18See [Datasources](../../datasources) for (much!) more information. 19 20### Usage 21 22```go 23datasource alias [subpath] 24``` 25 26### Arguments 27 28| name | description | 29|------|-------------| 30| `alias` | _(required)_ the datasource alias (or a URL for dynamic use) | 31| `subpath` | _(optional)_ the subpath to use, if supported by the datasource | 32 33### Examples 34 35_`person.json`:_ 36```json 37{ "name": "Dave" } 38``` 39 40```console 41$ gomplate -d person.json -i 'Hello {{ (datasource "person").name }}' 42Hello Dave 43``` 44 45## `datasourceExists` 46 47Tests whether or not a given datasource was defined on the commandline (with the 48[`--datasource/-d`](../../usage/#datasource-d) argument). This is intended mainly to allow 49a template to be rendered differently whether or not a given datasource was 50defined. 51 52Note: this does _not_ verify if the datasource is reachable. 53 54Useful when used in an `if`/`else` block. 55 56### Usage 57 58```go 59datasourceExists alias 60``` 61 62### Arguments 63 64| name | description | 65|------|-------------| 66| `alias` | _(required)_ the datasource alias | 67 68### Examples 69 70```console 71$ echo '{{if (datasourceExists "test")}}{{datasource "test"}}{{else}}no worries{{end}}' | gomplate 72no worries 73``` 74 75## `datasourceReachable` 76 77Tests whether or not a given datasource is defined and reachable, where the definition of "reachable" differs by datasource, but generally means the data is able to be read successfully. 78 79Useful when used in an `if`/`else` block. 80 81### Usage 82 83```go 84datasourceReachable alias 85``` 86 87### Arguments 88 89| name | description | 90|------|-------------| 91| `alias` | _(required)_ the datasource alias | 92 93### Examples 94 95```console 96$ gomplate -i '{{if (datasourceReachable "test")}}{{datasource "test"}}{{else}}no worries{{end}}' -d test=https://bogus.example.com/wontwork.json 97no worries 98``` 99 100## `defineDatasource` 101 102Define a datasource alias with target URL inside the template. Overridden by the [`--datasource/-d`](../../usage/#datasource-d) flag. 103 104Note: once a datasource is defined, it can not be redefined (i.e. if this function is called twice with the same alias, only the first applies). 105 106This function can provide a good way to set a default datasource when sharing templates. 107 108See [Datasources](../../datasources) for (much!) more information. 109 110### Usage 111 112```go 113defineDatasource alias url 114``` 115 116### Arguments 117 118| name | description | 119|------|-------------| 120| `alias` | _(required)_ the datasource alias | 121| `url` | _(required)_ the datasource's URL | 122 123### Examples 124 125_`person.json`:_ 126```json 127{ "name": "Dave" } 128``` 129 130```console 131$ gomplate -i '{{ defineDatasource "person" "person.json" }}Hello {{ (ds "person").name }}' 132Hello Dave 133$ FOO='{"name": "Daisy"}' gomplate -d person=env:///FOO -i '{{ defineDatasource "person" "person.json" }}Hello {{ (ds "person").name }}' 134Hello Daisy 135``` 136 137## `include` 138 139Includes the content of a given datasource (provided by the [`--datasource/-d`](../../usage/#datasource-d) argument). 140 141This is similar to [`datasource`](#datasource), except that the data is not parsed. There is no restriction on the type of data included, except that it should be textual. 142 143### Usage 144 145```go 146include alias [subpath] 147``` 148 149### Arguments 150 151| name | description | 152|------|-------------| 153| `alias` | _(required)_ the datasource alias, as provided by [`--datasource/-d`](../../usage/#datasource-d) | 154| `subpath` | _(optional)_ the subpath to use, if supported by the datasource | 155 156### Examples 157 158_`person.json`:_ 159```json 160{ "name": "Dave" } 161``` 162 163_`input.tmpl`:_ 164```go 165{ 166 "people": [ 167 {{ include "person" }} 168 ] 169} 170``` 171 172```console 173$ gomplate -d person.json -f input.tmpl 174{ 175 "people": [ 176 { "name": "Dave" } 177 ] 178} 179``` 180 181## `data.JSON` 182 183**Alias:** `json` 184 185Converts a JSON string into an object. Works for JSON Objects, but will 186also parse JSON Arrays. Will not parse other valid JSON types. 187 188For more explict JSON Array support, see [`data.JSONArray`](#data-jsonarray). 189 190#### Encrypted JSON support (EJSON) 191 192If the input is in the [EJSON](https://github.com/Shopify/ejson) format (i.e. has a `_public_key` field), this function will attempt to decrypt the document first. A private key must be provided by one of these methods: 193 194- set the `EJSON_KEY` environment variable to the private key's value 195- set the `EJSON_KEY_FILE` environment variable to the path to a file containing the private key 196- set the `EJSON_KEYDIR` environment variable to the path to a directory containing private keys (filename must be the public key), just like [`ejson decrypt`'s `--keydir`](https://github.com/Shopify/ejson/blob/master/man/man1/ejson.1.ronn) flag. Defaults to `/opt/ejson/keys`. 197 198### Usage 199 200```go 201data.JSON in 202``` 203```go 204in | data.JSON 205``` 206 207### Arguments 208 209| name | description | 210|------|-------------| 211| `in` | _(required)_ the input string | 212 213### Examples 214 215_`input.tmpl`:_ 216``` 217Hello {{ (getenv "FOO" | json).hello }} 218``` 219 220```console 221$ export FOO='{"hello":"world"}' 222$ gomplate < input.tmpl 223Hello world 224``` 225 226## `data.JSONArray` 227 228**Alias:** `jsonArray` 229 230Converts a JSON string into a slice. Only works for JSON Arrays. 231 232### Usage 233 234```go 235data.JSONArray in 236``` 237```go 238in | data.JSONArray 239``` 240 241### Arguments 242 243| name | description | 244|------|-------------| 245| `in` | _(required)_ the input string | 246 247### Examples 248 249_`input.tmpl`:_ 250``` 251Hello {{ index (getenv "FOO" | jsonArray) 1 }} 252``` 253 254```console 255$ export FOO='[ "you", "world" ]' 256$ gomplate < input.tmpl 257Hello world 258``` 259 260## `data.YAML` 261 262**Alias:** `yaml` 263 264Converts a YAML string into an object. Works for YAML Objects but will 265also parse YAML Arrays. This can be used to access properties of YAML objects. 266 267For more explict YAML Array support, see [`data.JSONArray`](#data-yamlarray). 268 269### Usage 270 271```go 272data.YAML in 273``` 274```go 275in | data.YAML 276``` 277 278### Arguments 279 280| name | description | 281|------|-------------| 282| `in` | _(required)_ the input string | 283 284### Examples 285 286_`input.tmpl`:_ 287``` 288Hello {{ (getenv "FOO" | yaml).hello }} 289``` 290 291```console 292$ export FOO='hello: world' 293$ gomplate < input.tmpl 294Hello world 295``` 296 297## `data.YAMLArray` 298 299**Alias:** `yamlArray` 300 301Converts a YAML string into a slice. Only works for YAML Arrays. 302 303### Usage 304 305```go 306data.YAMLArray in 307``` 308```go 309in | data.YAMLArray 310``` 311 312### Arguments 313 314| name | description | 315|------|-------------| 316| `in` | _(required)_ the input string | 317 318### Examples 319 320_`input.tmpl`:_ 321``` 322Hello {{ index (getenv "FOO" | yamlArray) 1 }} 323``` 324 325```console 326$ export FOO='[ "you", "world" ]' 327$ gomplate < input.tmpl 328Hello world 329``` 330 331## `data.TOML` 332 333**Alias:** `toml` 334 335Converts a [TOML](https://github.com/toml-lang/toml) document into an object. 336This can be used to access properties of TOML documents. 337 338Compatible with [TOML v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md). 339 340### Usage 341 342```go 343data.TOML input 344``` 345```go 346input | data.TOML 347``` 348 349### Arguments 350 351| name | description | 352|------|-------------| 353| `input` | _(required)_ the TOML document to parse | 354 355### Examples 356 357_`input.tmpl`:_ 358``` 359{{ $t := `[data] 360hello = "world"` -}} 361Hello {{ (toml $t).hello }} 362``` 363 364```console 365$ gomplate -f input.tmpl 366Hello world 367``` 368 369## `data.CSV` 370 371**Alias:** `csv` 372 373Converts a CSV-format string into a 2-dimensional string array. 374 375By default, the [RFC 4180](https://tools.ietf.org/html/rfc4180) format is 376supported, but any single-character delimiter can be specified. 377 378### Usage 379 380```go 381data.CSV [delim] input 382``` 383```go 384input | data.CSV [delim] 385``` 386 387### Arguments 388 389| name | description | 390|------|-------------| 391| `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 392| `input` | _(required)_ the CSV-format string to parse | 393 394### Examples 395 396_`input.tmpl`:_ 397``` 398{{ $c := `C,32 399Go,25 400COBOL,357` -}} 401{{ range ($c | csv) -}} 402{{ index . 0 }} has {{ index . 1 }} keywords. 403{{ end }} 404``` 405 406```console 407$ gomplate < input.tmpl 408C has 32 keywords. 409Go has 25 keywords. 410COBOL has 357 keywords. 411``` 412 413## `data.CSVByRow` 414 415**Alias:** `csvByRow` 416 417Converts a CSV-format string into a slice of maps. 418 419By default, the [RFC 4180](https://tools.ietf.org/html/rfc4180) format is 420supported, but any single-character delimiter can be specified. 421 422Also by default, the first line of the string will be assumed to be the header, 423but this can be overridden by providing an explicit header, or auto-indexing 424can be used. 425 426### Usage 427 428```go 429data.CSVByRow [delim] [header] input 430``` 431```go 432input | data.CSVByRow [delim] [header] 433``` 434 435### Arguments 436 437| name | description | 438|------|-------------| 439| `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 440| `header` | _(optional)_ comma-separated list of column names, set to `""` to get auto-named columns (A-Z), defaults to using the first line of `input` | 441| `input` | _(required)_ the CSV-format string to parse | 442 443### Examples 444 445_`input.tmpl`:_ 446``` 447{{ $c := `lang,keywords 448C,32 449Go,25 450COBOL,357` -}} 451{{ range ($c | csvByRow) -}} 452{{ .lang }} has {{ .keywords }} keywords. 453{{ end }} 454``` 455 456```console 457$ gomplate < input.tmpl 458C has 32 keywords. 459Go has 25 keywords. 460COBOL has 357 keywords. 461``` 462 463## `data.CSVByColumn` 464 465**Alias:** `csvByColumn` 466 467Like [`csvByRow`](#csvByRow), except that the data is presented as a columnar 468(column-oriented) map. 469 470### Usage 471 472```go 473data.CSVByColumn [delim] [header] input 474``` 475```go 476input | data.CSVByColumn [delim] [header] 477``` 478 479### Arguments 480 481| name | description | 482|------|-------------| 483| `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 484| `header` | _(optional)_ comma-separated list of column names, set to `""` to get auto-named columns (A-Z), defaults to using the first line of `input` | 485| `input` | _(required)_ the CSV-format string to parse | 486 487### Examples 488 489_`input.tmpl`:_ 490``` 491{{ $c := `C;32 492Go;25 493COBOL;357` -}} 494{{ $langs := ($c | csvByColumn ";" "lang,keywords").lang -}} 495{{ range $langs }}{{ . }} 496{{ end -}} 497``` 498 499```console 500$ gomplate < input.tmpl 501C 502Go 503COBOL 504``` 505 506## `data.ToJSON` 507 508**Alias:** `toJSON` 509 510Converts an object to a JSON document. Input objects may be the result of `json`, `yaml`, `jsonArray`, or `yamlArray` functions, or they could be provided by a `datasource`. 511 512### Usage 513 514```go 515data.ToJSON obj 516``` 517```go 518obj | data.ToJSON 519``` 520 521### Arguments 522 523| name | description | 524|------|-------------| 525| `obj` | _(required)_ the object to marshal | 526 527### Examples 528 529_This is obviously contrived - `json` is used to create an object._ 530 531_`input.tmpl`:_ 532``` 533{{ (`{"foo":{"hello":"world"}}` | json).foo | toJSON }} 534``` 535 536```console 537$ gomplate < input.tmpl 538{"hello":"world"} 539``` 540 541## `data.ToJSONPretty` 542 543**Alias:** `toJSONPretty` 544 545Converts an object to a pretty-printed (or _indented_) JSON document. 546Input objects may be the result of functions like `data.JSON`, `data.YAML`, 547`data.JSONArray`, or `data.YAMLArray` functions, or they could be provided 548by a [`datasource`](../general/datasource). 549 550The indent string must be provided as an argument. 551 552### Usage 553 554```go 555data.ToJSONPretty indent obj 556``` 557```go 558obj | data.ToJSONPretty indent 559``` 560 561### Arguments 562 563| name | description | 564|------|-------------| 565| `indent` | _(required)_ the string to use for indentation | 566| `obj` | _(required)_ the object to marshal | 567 568### Examples 569 570_`input.tmpl`:_ 571``` 572{{ `{"hello":"world"}` | data.JSON | data.ToJSONPretty " " }} 573``` 574 575```console 576$ gomplate < input.tmpl 577{ 578 "hello": "world" 579} 580``` 581 582## `data.ToYAML` 583 584**Alias:** `toYAML` 585 586Converts an object to a YAML document. Input objects may be the result of 587`data.JSON`, `data.YAML`, `data.JSONArray`, or `data.YAMLArray` functions, 588or they could be provided by a [`datasource`](../general/datasource). 589 590### Usage 591 592```go 593data.ToYAML obj 594``` 595```go 596obj | data.ToYAML 597``` 598 599### Arguments 600 601| name | description | 602|------|-------------| 603| `obj` | _(required)_ the object to marshal | 604 605### Examples 606 607_This is obviously contrived - `data.JSON` is used to create an object._ 608 609_`input.tmpl`:_ 610``` 611{{ (`{"foo":{"hello":"world"}}` | data.JSON).foo | data.ToYAML }} 612``` 613 614```console 615$ gomplate < input.tmpl 616hello: world 617``` 618 619## `data.ToTOML` 620 621**Alias:** `toTOML` 622 623Converts an object to a [TOML](https://github.com/toml-lang/toml) document. 624 625### Usage 626 627```go 628data.ToTOML obj 629``` 630```go 631obj | data.ToTOML 632``` 633 634### Arguments 635 636| name | description | 637|------|-------------| 638| `obj` | _(required)_ the object to marshal as a TOML document | 639 640### Examples 641 642```console 643$ gomplate -i '{{ `{"foo":"bar"}` | data.JSON | data.ToTOML }}' 644foo = "bar" 645``` 646 647## `data.ToCSV` 648 649**Alias:** `toCSV` 650 651Converts an object to a CSV document. The input object must be a 2-dimensional 652array of strings (a `[][]string`). Objects produced by [`data.CSVByRow`](#conv-csvbyrow) 653and [`data.CSVByColumn`](#conv-csvbycolumn) cannot yet be converted back to CSV documents. 654 655**Note:** With the exception that a custom delimiter can be used, `data.ToCSV` 656outputs according to the [RFC 4180](https://tools.ietf.org/html/rfc4180) format, 657which means that line terminators are `CRLF` (Windows format, or `\r\n`). If 658you require `LF` (UNIX format, or `\n`), the output can be piped through 659[`strings.ReplaceAll`](../strings/#strings-replaceall) to replace `"\r\n"` with `"\n"`. 660 661### Usage 662 663```go 664data.ToCSV [delim] input 665``` 666```go 667input | data.ToCSV [delim] 668``` 669 670### Arguments 671 672| name | description | 673|------|-------------| 674| `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 675| `input` | _(required)_ the object to convert to a CSV | 676 677### Examples 678 679_`input.tmpl`:_ 680```go 681{{ $rows := (jsonArray `[["first","second"],["1","2"],["3","4"]]`) -}} 682{{ data.ToCSV ";" $rows }} 683``` 684 685```console 686$ gomplate -f input.tmpl 687first,second 6881,2 6893,4 690``` 691