Mutating JsonElement would not work.
In case you want everything to be done explicitly, you might consider deserializing the json, editing and serializing back if needed.
This way you'll also be sure that the incoming data always has the structure you need
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace Deserializer
{
// This is your custom structure. I've called it UserInfo, but feel free to rename it
public class UserInfo
{
public string Name { get; set; }
public string Reference { get; set; }
}
public class Program
{
public static void Main()
{
// Sample data
string jsonString =
@"[{""name"":""Jones"",""reference"":""6546""},{""name"":""James"",""reference"":""9631""},{""name"":""Rita"",""reference"":""8979""}]";
// Deserializing the json into a list of objects
var userInfoList = JsonSerializer.Deserialize<List<UserInfo>>(jsonString);
foreach (var userInfo in userInfoList) {
// Modifying the data
userInfo.Reference = "test";
}
// Converting back to json string in case it's needed.
var modifiedJsonString = JsonSerializer.Serialize(userInfoList);
Console.WriteLine(modifiedJsonString);
}
}
}