tidwall / sjson

Set JSON values very quickly in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: Set a value with an object/array

christoth opened this issue · comments

Would it be possible to set a value to an object/array?
So don't escape the string value if it starts with "{" | "[" and ends with "}" | "]".

value, _ := sjson.Set(`{"name":"Andy"}`, "friends", `["Carol","Sara"]')
println(value)

// Output:
// {"name":"Andy","friends":["Carol","Sara"]}
value, _ := sjson.Set(`{"name":"Andy"}`, "meta", `{"gender":"M","age":42}`)
println(value)

// Output:
// {"name":"Andy","meta":{"gender":"M","age":42}}

Or append an object/array to an existing array?

value, _ := sjson.Set(`{"friends":[{"name":"Andy","age": 8},{"name":"Carol","age":7}]}`, "friends.-1", `{"name":"Sara","age":8}`)
println(value)

// Output:
// {"friends":[{"name":"Andy","age": 8},{"name":"Carol","age":7},{"name":"Sara","age":8}]}

Thanks in advance!

I think the SetRaw function might be what you are looking for.

That worked perfectly, thank you!

Would you like me to create a PR to add SetRaw to the readme doc?