Skip to content

Errors in parsing simple JSON #4

Description

@FZambia

@buger hello, nice package idea, keep up a good work!

Just tried example from readme and got a couple of errors, here is code to reproduce:

package main

import (
    "fmt"

    "github.com/buger/jsonparser"
)

func main() {

    data := []byte(`{
  "person": {
    "married": true
  },
  "company": {
    "name": "Acme",
    "size": 109
  }
}`)

    // If the key doesn't exist it will throw an error
    var size float64 = 0
    if value, _, err := jsonparser.GetNumber(data, "company", "size"); err == nil {
        size = value
    } else {
        fmt.Println(err.Error())
    }
    fmt.Println(size)

    var married bool
    if value, _, err := jsonparser.GetBoolean(data, "person", "married"); err == nil {
        married = value
    } else {
        fmt.Println(err.Error())
    }
    fmt.Println(married)
}

Output:

strconv.ParseFloat: parsing "109\n  ": invalid syntax
0
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/buger/jsonparser.GetBoolean(0xc820058070, 0x61, 0x70, 0xc82003bf28, 0x2, 0x2, 0x0, 0x26, 0x0, 0x0)
    /private/var/www/different/go/gopath/src/github.com/buger/jsonparser/parser.go:286 +0x28a
main.main()
    /private/var/www/different/go/gopath/src/github.com/centrifugal/parse.go:30 +0x2a9
exit status 2

Seems like

  1. some values need proper trimming
  2. I think there must be v[0] on this line

Also example is broken, instead of

// If the key doesn't exist it will throw an error
size := 0
if value, _, err := jsonparser.GetNumber(data, "company", "size"); err != nil {
  size = value
}

There must be sth like this I suppose:

// If the key doesn't exist it will throw an error
var size float64 = 0
if value, _, err := jsonparser.GetNumber(data, "company", "size"); err == nil {
  size = value
}

Also I found confusing return in tests

Sorry for not sending pr!

Activity

  1. buger commented on Mar 22, 2016

    @buger
    Owner

    Thank you for feedback! Everything should be fixed now.

  2. FZambia commented on Mar 22, 2016

    @FZambia
    ContributorAuthor

    Unfortunately not - have you tried to run my example? It still panics for boolean key.

    Also you fixed readme but not completely, see:

    if value, _, err := jsonparser.GetNumber(data, "company", "size"); err != nil {
    

    Should be

    if value, _, err := jsonparser.GetNumber(data, "company", "size"); err == nil {
    
  3. added a commit that references this issue on May 3, 2026
    80cf537
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions