tidwall / sjson

Set JSON values very quickly in Go
MIT License
2.4k stars 165 forks source link

Modify a data from list based on condition #46

Closed iamshreeram closed 3 years ago

iamshreeram commented 3 years ago

Hi, I would want to modify a specific data from Slice of JSON. Is it possible in sjson?

{
    "app": "gfgeeks",
    "prop": [
          {"region": 736,"set": true,"score": 72},
          {"region": 563,"set": true,"score": 333},     // modify score to 334; if region == 563 && set == true
          {"region": 563,"set": false,"score": 333}
    ],
    "index" : "haskell"
}

I'm currently using gjson to get the json object and I'm able to modify the score to 334. While trying to replace the json back in list, I would want to delete {"region": 563,"set": true,"score": 333} element and add {"region": 563,"set": true,"score": 334}. Currently, sjson is letting me to delete only positional parameter. For example, I could do sjson.Delete(JSON, prop.1) would work. But, the position would often vary. So, I cannot use this approach.

Playground : https://play.golang.org/p/lZu9LezD3Wn

@tidwall

tidwall commented 3 years ago

This looks to be related to https://github.com/tidwall/gjson/issues/218. I answered that issue moments ago. Let me know if my solution helps.

iamshreeram commented 3 years ago

Yes. Thanks!