Skip to content

Instantly share code, notes, and snippets.

@SimonStorlSchulke
Created March 21, 2023 00:23
Show Gist options
  • Save SimonStorlSchulke/1db850508848d7c300859143d6db4954 to your computer and use it in GitHub Desktop.
Save SimonStorlSchulke/1db850508848d7c300859143d6db4954 to your computer and use it in GitHub Desktop.
Replace mutiple json values at the given key-array
function replaceJsonArguments(jsonText: string, keyValues: Map<string[], string>) {
let jsonObj = JSON.parse(jsonText);
for (let [key, value] of keyValues) {
var currentObj = jsonObj;
for (var i = 0; i < key.length - 1; i++) {
currentObj = currentObj[key[i]];
}
currentObj[key[key.length - 1]] = value; //ChatGPT is a genius...
}
}
@SimonStorlSchulke
Copy link
Author

FE:

replaceJsonArguments(
      "jsontext... (won't paste it in here)",
      new Map<string[], string>([
        [["Backend", "ApiKey"], "324iuwhej"],
        ["BaseUrl"], "Test.com"],
      ])
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment