Skip to content

Set for each entry in array #112

Description

@Nighthawk22

Hello!

Would it be possible to add the possibility to set the value for each element in the array like this:
jsonparser.Set(jsonBody, []byte("\"\""), "issuesData", "issues", "[]", "extraFields", "[]", "html")

Thanks in advance,
Nighthawk

Activity

  1. ajeecai commented on Mar 11, 2018

    @ajeecai

    Hi,

    I am looking for the same, which allows to replace some entry in array on the fly. My app just receives json data from server as following and want to modify it in place:
    "content":
    ...
    "list": [
    {
    "name": "abc",
    ...
    },
    {
    "name": "xyz",
    ...
    }
    ...
    ]

    I can get the name from array, with the follow code snippet. If I dump the return byte slice val, it looks Set has taken effect but I can't set it back to the original slice (body). Any suggestion? Thanks

                     jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
                         log.Println(string(value))
                         v, rErr := jsonparser.GetString(value, "name")
                         if rErr == nil {
                             log.Println(offset, v)
                         } else {
                             log.Println(rErr)
                         }
                         val, wErr := jsonparser.Set(value, []byte("An example"),"name")
                         if wErr == nil {
                          value = val
                         } else {
                             log.Println(wErr)
                         }
                     }, "content", "list")
    
  2. sergolius commented on May 7, 2018

    @sergolius

    @Nighthawk22 , @ajeecai
    What about do it like:

    data := `[{"key":123}]`
    byteData := []byte(data)
    i := 0
    jsonparser.ArrayEach(byteData, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
    	byteData, _ = jsonparser.Set(byteData, []byte("124"), fmt.Sprintf("[%d]", i), "key")
    	i++
    })
    
  3. added a commit that references this issue on Jul 28, 2026
    f6ddf26
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