
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
github.com/higress-group/gjson_template
A powerful template engine based on Go's standard template library, enhanced with GJSON path syntax support for more flexible and efficient JSON processing without reflection.
GJSON Template is a fork of Go's standard template package that replaces the reflection-based value lookup mechanism with GJSON path syntax. This provides several advantages:
The library accepts JSON data as []byte
instead of interface{}
, allowing for direct parsing with GJSON while preserving all the template features like conditionals, loops, and variable assignments.
go get github.com/higress-group/gjson_template
Here's a simple example demonstrating how to use GJSON Template:
package main
import (
"fmt"
"os"
template "github.com/higress-group/gjson_template"
)
func main() {
// JSON data as a string
jsonData := []byte(`{
"name": "John Doe",
"age": 30,
"address": {
"city": "New York",
"country": "USA"
},
"skills": ["Go", "JavaScript", "Python"]
}`)
// Create a template with GJSON path syntax
tmpl, err := template.New("example").Parse(`
Name: {{.name}}
Age: {{.age}}
City: {{.address.city}}
First Skill: {{.skills.0}}
All Skills: {{range .skills}}{{.}}, {{end}}
`)
if err != nil {
fmt.Printf("Error parsing template: %v\n", err)
return
}
// Execute the template with JSON data
err = tmpl.Execute(os.Stdout, jsonData)
if err != nil {
fmt.Printf("Error executing template: %v\n", err)
}
}
GJSON Template supports all of GJSON's powerful path syntax. Here's an example showcasing some advanced features:
package main
import (
"fmt"
"os"
template "github.com/higress-group/gjson_template"
)
func main() {
// JSON data with more complex structure
jsonData := []byte(`{
"users": [
{"name": "Alice", "age": 28, "active": true, "roles": ["admin", "developer"]},
{"name": "Bob", "age": 35, "active": false, "roles": ["developer"]},
{"name": "Charlie", "age": 42, "active": true, "roles": ["manager", "developer"]}
],
"settings": {
"theme": "dark",
"notifications": {
"email": true,
"sms": false
}
}
}`)
// Template with advanced GJSON path syntax
tmpl, err := template.New("advanced").Parse(`
<!-- Using array indexing -->
First user: {{.users.0.name}}
<!-- Using the gjson function for more complex queries -->
Active users: {{gjson "users.#(active==true)#.name"}}
<!-- Array filtering with multiple conditions -->
Active developers over 30: {{gjson "users.#(active==true && age>30)#.name"}}
<!-- Using modifiers -->
User names (reversed): {{gjson "users.@reverse.#.name"}}
<!-- Working with nested properties -->
Email notifications: {{if .settings.notifications.email}}Enabled{{else}}Disabled{{end}}
<!-- Iterating over filtered results -->
Admins:
{{range $user := gjson "users.#(roles.#(==admin)>0)#"}}
- {{$user.name}} ({{$user.age}})
{{end}}
`)
if err != nil {
fmt.Printf("Error parsing template: %v\n", err)
return
}
err = tmpl.Execute(os.Stdout, jsonData)
if err != nil {
fmt.Printf("Error executing template: %v\n", err)
}
}
For even more complex examples, check out the markdown.go file in the example directory, which demonstrates how to convert a JSON blog post into a Markdown document using advanced GJSON path features.
GJSON Template supports the full GJSON path syntax. Here are some key features:
address.city
users.0.name
users.#.name
users.*.name
users.#(age>=30)#.name
users.@reverse.#.name
{name:users.0.name,count:users.#}
path.with\.dot
For a complete reference of GJSON path syntax, see the GJSON documentation.
GJSON Template comes with all of Sprig's functions built-in, providing a rich set of over 70 template functions for string manipulation, math operations, date formatting, list processing, and more. This makes GJSON Template functionally equivalent to Helm's template capabilities.
Some commonly used Sprig functions include:
trim
, upper
, lower
, replace
, plural
, nospace
add
, sub
, mul
, div
, max
, min
now
, date
, dateInZone
, dateModify
list
, first
, last
, uniq
, sortAlpha
dict
, get
, set
, hasKey
, pluck
ternary
, default
, empty
, coalesce
toString
, toJson
, toPrettyJson
, toRawJson
b64enc
, b64dec
, urlquery
, urlqueryescape
uuidv4
Example usage:
// String manipulation
{{lower .title | replace " " "-"}} // Convert to lowercase and replace spaces with hyphens
// Math operations
{{add 5 .count}} // Add 5 to the count value
// Date formatting
{{now | date "2006-01-02"}} // Format current date as YYYY-MM-DD
// List operations
{{list 1 2 3 | join ","}} // Create a list and join with commas
For a complete reference of all available functions, see the Helm documentation on functions, as GJSON Template includes the same function set.
When working with AI assistants to generate templates using GJSON Template, you can use the following prompt to help the AI understand the syntax:
When generating Go templates for the GJSON Template library, please follow these guidelines:
1. The library is based on Go's standard template package but uses GJSON for JSON parsing.
2. JSON data is passed as []byte instead of interface{}, and values are accessed using GJSON path syntax.
3. Basic dot notation works as in standard templates: {{.user.name}}
4. For complex queries, use the gjson function:
- Simple path: {{gjson "users.0.name"}}
- Array filtering: {{gjson "users.#(active==true)#.name"}}
- Using modifiers: {{gjson "users.@reverse.#.name"}}
- Multipath: {{gjson "{name:users.0.name,count:users.#}"}}
5. All standard template features work normally:
- Conditionals: {{if .condition}}...{{else}}...{{end}}
- Loops: {{range .items}}...{{end}}
- Variables: {{$var := .value}}
- Functions: {{len .array}}
6. When using backticks for GJSON paths, escape any internal quotes:
{{gjson `users.#(name=="John").age`}}
7. For complex paths with special characters, use double quotes with escaping:
{{gjson "path.with\\.dot"}}
Please generate a template that processes JSON data according to these guidelines.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.