Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set for each entry in array #112

Open
Nighthawk22 opened this issue Aug 10, 2017 · 2 comments
Open

Set for each entry in array #112

Nighthawk22 opened this issue Aug 10, 2017 · 2 comments

Comments

@Nighthawk22
Copy link

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

@ajeecai
Copy link

ajeecai commented Mar 11, 2018

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")

@sergolius
Copy link

@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++
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants