I have a simple JSON I would like to rename (probably by copying then removing) a key. .NET Fiddle:
const string input = @"{ ""foo"": [{""a"": 1}, {""b"": null}] }";
var node = JsonNode.Parse(input);
Console.WriteLine(node); // Output: the JSON
Console.WriteLine(node["foo"][0]["a"]); // Output: 1
// How to copy/rename?
node["bar"] = node["foo"];
Console.WriteLine(node["bar"][0]["a"]); // Should be 1
{
"foo": [
{
"a": 1
},
{
"b": null
}
]
}
How do I copy/rename it? I tried using node["foo"].AsValue()
but it doesn't work as well. The error is
The node must be of type 'JsonValue'
I just found a workaround by converting to JSON string and parsing it again but I hope there is a better way to do it.
node["bar"] = JsonNode.Parse(node["foo"].ToJsonString());