jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

Question: How to update JSON at path

Open zerodefect opened this issue 1 year ago • 1 comments

I'm new to golang, so my question probably more stems from a lack of knowledge, but I'm getting there.

I have a small JSON sample that looks like the following:

{
    "FullOTAConfig": {
        "Licensing": {
            "isLicensed": true,
            "canTerminate": false
        },
        "BirdGlobal": {},
        "Tenants": {
            "Tenant1_Config": null,
            "Tenant2_Config": null,
            "Tenant3_Config": null
        }
    }
}

I am able to retrieve the JSON that I'm interested at path $.FullOTAConfig.Licensing via:

  // JSON map interface
  jsonInput map[string]interface{}
  
  // Load some JSON
  // ... 
  // ...
  
  // Get JSON at the requested path
  subJson, err := jsonpath.Get(jsonPath, jsonInput)
  if err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
  
  // How do I update the JSON at path 'jsonPath'?
  // ... ???
  // jsonInput[?] = new_json_object

but what I would like to do is once the subobject is returned, I want to update the 'jsonInput' var but I don't know how I can use the jsonpath to then update the JSON in the map based on the path.

zerodefect avatar Jun 10 '23 16:06 zerodefect

I'm also interested in that. Have you found a way to update the json at path?

mdzhigarov avatar Apr 03 '24 08:04 mdzhigarov