json
v0.0.0-20260504031610-00e60cb5517f
Documentation
pkg.go.dev ↗Package json decodes JSON.
Constants
ObjectStart (10 declarations)
const (
ObjectStart = '{' // {
ObjectEnd = '}' // }
String = '"' // "
Colon = ':' // :
Comma = ',' // ,
ArrayStart = '[' // [
ArrayEnd = ']' // ]
True = 't' // t
False = 'f' // f
Null = 'n' // n
)
Types
Decoder
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder decodes JSON values from an input stream.
NewDecoder
func NewDecoder(r io.Reader) *Decoder
NewDecoder returns a new Decoder for the supplied Reader r.
NewDecoderBuffer
func NewDecoderBuffer(r io.Reader, buf []byte) *Decoder
NewDecoderBuffer returns a new Decoder for the supplier Reader r, using the []byte buf provided for working storage.
Decoder.Decode
func (d *Decoder) Decode(v any) error
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
Example
{
input := `{"a": 1,"b": 123.456, "c": [null]}`
dec := json.NewDecoder(strings.NewReader(input))
var i any
err := dec.Decode(&i)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", i)
}
Output:
map[a:1 b:123.456 c:[<nil>]]
Decoder.NextToken
func (d *Decoder) NextToken() ([]byte, error)
NextToken returns a []byte referencing the next logical token in the stream. The []byte is valid until Token is called again. At the end of the input stream, Token returns nil, io.EOF.
Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.
A valid token begins with one of the following:
{ Object start
[ Array start
} Object end
] Array End
t JSON true
f JSON false
n JSON null
" A string, possibly containing backslash escaped entites.
-, 0-9 A number
Commas and colons are elided.
Example
{
input := `{"a": 1,"b": 123.456, "c": [null]}`
dec := json.NewDecoder(strings.NewReader(input))
for {
tok, err := dec.NextToken()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", tok)
}
}
Output:
{
"a"
1
"b"
123.456
"c"
[
null
]
}
Decoder.Token
func (d *Decoder) Token() (json.Token, error)
Token returns the next JSON token in the input stream. At the end of the input stream, Token returns nil, io.EOF.
Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.
The input stream consists of basic JSON values—bool, string, number, and null—along with delimiters [ ] { } of type json.Delim to mark the start and end of arrays and objects. Commas and colons are elided.
Note: this API is provided for compatibility with the encoding/json package and carries a significant allocation cost. See NextToken for a more efficient API.
Example
{
input := `{"a": 1,"b": 123.456, "c": [null]}`
dec := json.NewDecoder(strings.NewReader(input))
for {
tok, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", tok)
}
}
Output:
{
a
1
b
123.456
c
[
<nil>
]
}
Scanner
type Scanner struct {
// contains filtered or unexported fields
}
Scanner implements a JSON scanner as defined in RFC 7159.
NewScanner
func NewScanner(r io.Reader) *Scanner
NewScanner returns a new Scanner for the io.Reader r. A Scanner reads from the supplied io.Reader and produces via Next a stream of tokens, expressed as []byte slices.
Scanner.Error
func (s *Scanner) Error() error
Error returns the first error encountered. When underlying reader is exhausted, Error returns io.EOF.
Scanner.Next
func (s *Scanner) Next() []byte
Next returns a []byte referencing the the next lexical token in the stream. The []byte is valid until Next is called again. If the stream is at its end, or an error has occurred, Next returns a zero length []byte slice.
A valid token begins with one of the following:
{ Object start
[ Array start
} Object end
] Array End
, Literal comma
: Literal colon
t JSON true
f JSON false
n JSON null
" A string, possibly containing backslash escaped entites.
-, 0-9 A number
Example
{
input := `{"a": 1,"b": 123.456, "c": [null]}`
sc := json.NewScanner(strings.NewReader(input))
for {
tok := sc.Next()
if len(tok) < 1 {
break
}
fmt.Printf("%s\n", tok)
}
if err := sc.Error(); err != nil && err != io.EOF {
log.Fatal(err)
}
}
Output:
{
"a"
:
1
,
"b"
:
123.456
,
"c"
:
[
null
]
}