tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.21k stars 847 forks source link

Exists methods returns true if the array is empty #283

Open SriHemnath opened 2 years ago

SriHemnath commented 2 years ago
{
    "data":{
        "outcomes":[
            {
            }
        ]
    }
}

Outcomes is an empty array and if we call Exists() it will return true because it will have [] in it and len function returns true. It should return false as array is empty.

data, err := ioutil.ReadFile("array.json")
    if err != nil {
        fmt.Println("Error while reading file ", err)
    }
    fmt.Println("Read successfully: ", string(data))

    actionTypeEntries := gjson.GetBytes(data, "data.outcomes.#.action.type")
    fmt.Println(len(actionTypeEntries.Raw))
    if actionTypeEntries.Exists() {
        fmt.Println("has value")
    } else {
        fmt.Println("No values")
    }

This PR is to fix this issue.

tidwall commented 2 years ago

The actionTypeEntries results in the empty array [].

An empty array technically exists, even though it's empty.