I have a json file that looks something like this:
{
"a":{
"lot":"of",
"random":"stuff"
},
"url":"https://a.url.I.want.to.change",
"some":{
"other":"very",
"random":"stuff"
}
}
I read that json from a file and I want to change exactly the url
field and than I want to save it back to the file.
I am using System.Text.Json
in the entire project and I don´t want to introduce a dependency to newtonsoft.json
I tried it using dynamic
like this:
string json = File.ReadAllText(filepath);
dynamic settings = JsonSerializer.Deserialize<dynamic>(json);
settings.url = "https://my.new.url"; // <- that is the problem
File.WriteAllText(filepath, JsonSerializer.Serialize(settings));
and also like this:
settings["url"] = "https://my.new.url";
but none of the approaches work because JsonSerializer
deserializes it into an JsonElement
, which is readonly I think.
Has anyone an idea of how that could be solved?