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

Need to set array of values to array of objects. #57

Open
Rushi-Kumar opened this issue Apr 12, 2022 · 1 comment
Open

Need to set array of values to array of objects. #57

Rushi-Kumar opened this issue Apr 12, 2022 · 1 comment

Comments

@Rushi-Kumar
Copy link

need to set multiple values for an array of objects

package main

import (
	"fmt"

	"github.com/tidwall/sjson"
)

const jsondata = `{"data":[]}`

func main() {
	data := make([]string, 0)
	data = append(data, "sos", "msa", "kkh")
	value, err := sjson.Set(jsondata, "data.#.name", data)
	if err != nil {
		fmt.Println(err)
	}
	println(value)

}

expected result

{data:[{name:"sos"},{name:"msa"},{name:"kkh"}]

output result

{"data":[]}

from my understanding of the source code, I have observed that the value is converted to a string and replaced at the exact path.
is there any possible way to implement this?. However, we will be able to map the array by providing an index number instead of #

value, err := sjson.Set(jsondata, "data.0.name", data[0])
value, err = sjson.Set(value, "data.1.name", data[1])
@BeforyDeath
Copy link

BeforyDeath commented Jun 1, 2022

jsonData := `{"data":[]}`

data := make([]string, 0)
data = append(data, "sos", "msa", "kkh")

for _, v := range data {
	jsonData, _ = sjson.Set(jsonData, "data.-1.name", v)
}

println(jsonData)

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

2 participants