Skip to content

Handle errors from ArrayEach callback #129

Description

@ProgramCpp

Is there a way for the callback to ArrayEach to return errors.

_,errr := jsonparser.ArrayEach(bytes, func(... error) {
              return myerror
},...) 

if errr == myerror 

Activity

  1. Yokii908 commented on Apr 12, 2018

    @Yokii908

    You can declare an error variable above your ArrayEach block then assign it in your callback so you can check if nil afterwards.

  2. cludden commented on Aug 6, 2018

    @cludden

    @Yokii908 i've been using your approach, but wondering if there is a way to stop iteration on error? In my case, if I'm processing a large array and encounter an error, i would like to exit and return the error instead of continuing to iterate through a large array. Currently I do

    var innererr error
    _, err := jsonparser.ArrayEach(bytes, func(data []byte, _ jsonparser.ValueType, _ int, _ error) {
        // exit early if an error has already occurred
        if innererr != nil {
            return
        }
    
        // do heavy processing and set innererr if an error occurs
        if err := doHeavyWork(data); err != nil {
            innererr = err
            return
        }
    }, "path", "to", "large", "array")

    if there is a way to simply return the error and stop iteration, it's not obvious to me.

  3. buger commented on Jul 28, 2026

    @buger
    Owner

    Implemented in v1.4.0 — ArrayEachErr lets the callback return an error to stop iteration early. See: https://github.com/buger/jsonparser/releases/tag/v1.4.0

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