sjson icon indicating copy to clipboard operation
sjson copied to clipboard

How to override or set the json value at the root of the json object ?

Open manmohantarle opened this issue 1 year ago • 2 comments

Hello @tidwall

I want to achieve the following use cases using sjson.SetRaw function use case 1 : initialJson := [] or ``(empty json) inputJson := [1,2,3]

resultJson should be [1,2,3]

use case 2: initialJson := {} or ``(empty json) inputJson := {"test":[1,2,3],"test2":"testval"}

resultJson should be {"test":[1,2,3],"test2":"testval"}

what should be the path value to achieve this using the sjson.SetRaw function ?

Thanks in advance !

manmohantarle avatar Mar 07 '24 13:03 manmohantarle

From the looks of the upstream library, I think you can use the path "@this" for the root. I'm trying to test that.

shearn89 avatar Jun 06 '25 14:06 shearn89

I had tried @this, but it does not work.

`package main

import "github.com/tidwall/sjson"

const json = {}

func main() { value, _ := sjson.SetRaw(json, "@this", "{"test":true}") println(value) }`

outputs {}

manmohantarle avatar Jun 09 '25 14:06 manmohantarle