mirror of
https://github.com/luxfi/netrunner.git
synced 2026-10-30 15:24:46 +00:00
29 lines
611 B
Go
29 lines
611 B
Go
// Copyright (C) 2021-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package utils
|
|
|
|
import "encoding/json"
|
|
|
|
// Set k=v in JSON string
|
|
// e.g., "track-subnets" is the key and value is "a,b,c".
|
|
func SetJSONKey(jsonBody string, k string, v string) (string, error) {
|
|
var config map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(jsonBody), &config); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if v == "" {
|
|
delete(config, k)
|
|
} else {
|
|
config[k] = v
|
|
}
|
|
|
|
updatedJSON, err := json.Marshal(config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(updatedJSON), nil
|
|
}
|