To integrate with an API I need my C# to output the following json:
"fields": {  
    "name1": {  
        "key1": "value1",
        "key2": "value2"
    },
    "name2": {  
        "key3": "value3",
        "key4": "value4"
    },
    "etc..."
}
I don't understand how to set this up.
Currently I'm using a class which I then serialize: JsonConvert.SerializeObject(document).
I have tried the following code:
public class Fields
{
    public string Name { get; internal set; }
         
    public Field myField { get; internal set; }
    public class Field
    {
        public string Value { get; internal set; }
        public string Key { get; internal set; }
        public Field(string value, string key)
        {
            Value = value;
            Key = key;
        }
    }
    public Fields(string name, Field myField)
    {
        Name = name;
        this.myField = myField;
    }
}
List<Fields> myFields = new List<Fields>();
foreach (var field in recipient.Fields)
    {
        myFields.Add(new Fields(field, new Fields.Field(name, value)));
    }
document.Fields = myFields;
But that results in:
"fields": [
    {
      "Name": "name1",
      "myField": {
        "key1": "value1",
        "key2": "value2"
      }
    },
    {
      "Name": "name2",
      "myField": {
        "key3": "value3",
        "key4": "value4"
      }
    }
]
The square brackets around the collection of fields need to be gone, and where it says "myField" it should be replaced by the variable "name1", "name2", etc.
Edit: I was mistaken, ignore the following part. It is possible for names to repeat themselves, the combination of the name and the second variable must be unique.
I could manually create the correct string with the given variables, but I feel like there has to be a better, "correct" way to achieve this.
Edit: fiddle
 
     
    