Configs2026年7月18日·1 分钟阅读

GJSON — Fast JSON Value Retrieval for Go

A Go package that provides a fast and simple way to get values from a JSON document using a dot-notation path syntax, without unmarshaling.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
GJSON Overview
直接安装命令
npx -y tokrepo@latest install 4ded98da-8242-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

GJSON is a Go package that lets you retrieve values from JSON strings using a path syntax, without needing to unmarshal into structs or maps. It is designed for speed and simplicity when you only need to read specific fields from a JSON payload.

What GJSON Does

  • Retrieves values from JSON strings using dot-separated paths like name.first
  • Supports array indexing, wildcards, and nested queries
  • Parses JSON lazily — only scans what is needed to find the target path
  • Returns typed results with methods like .String(), .Int(), .Float(), .Bool()
  • Works with raw JSON bytes or strings, no allocation-heavy unmarshaling

Architecture Overview

GJSON operates as a single-pass scanner over the raw JSON byte slice. When you call gjson.Get(), it walks the JSON from left to right, matching path components as it encounters keys. Once the target is found, it returns a Result value that wraps the raw JSON segment. This lazy approach avoids parsing the entire document and minimizes allocations.

Self-Hosting & Configuration

  • Install with go get github.com/tidwall/gjson — pure Go, zero dependencies
  • Import and call gjson.Get(jsonString, "path") — no setup needed
  • For multiple lookups, use gjson.GetMany() to scan once for several paths
  • Works on []byte via gjson.GetBytes() to avoid string conversion overhead
  • Pair with SJSON (same author) for setting values in JSON

Key Features

  • Path syntax supports modifiers like @reverse, @flatten, @valid
  • Multipath queries retrieve multiple values in a single scan
  • Custom modifiers can be registered for domain-specific transformations
  • No reflection or code generation — works with raw strings at runtime
  • Consistently outperforms encoding/json unmarshal for partial reads

Comparison with Similar Tools

  • encoding/json — full unmarshal into structs; GJSON is faster when you only need a few fields
  • jsonparser — similar lazy parsing, but GJSON offers richer path syntax and modifiers
  • bytedance/sonic — full JSON codec with JIT; GJSON focuses on read-only path queries
  • jq — command-line JSON processor; GJSON is an embeddable Go library

FAQ

Q: Is GJSON safe for concurrent use? A: Yes, gjson.Get is a pure function with no shared state.

Q: Can GJSON modify JSON? A: No, GJSON is read-only. Use SJSON (github.com/tidwall/sjson) for mutations.

Q: How does it handle invalid JSON? A: It returns a zero-value Result. Use gjson.Valid() to check validity first.

Q: Does GJSON support JSON5 or comments? A: No, it only supports standard JSON (RFC 8259).

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产