skip to Main Content

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

2

Answers


  1. You can use Dictionary<string, Dictionary<string, string>> and create a model with this structure:

    public class Fields
    {
        public Dictionary<string, Dictionary<string, string>> Items { get; set; }
    }
    

    When a dictionary is serialized into JSON, the keys are used as property names and the values are used as property values. So to reproduce your JSON structure in your code, you need to initialize the model as follows:

    var fields = new Fields();
    
    var item1 = new Dictionary<string, string>()
    {
        ["key1"] = "value1",
        ["key2"] = "value2"
    };
    
    var item2 = new Dictionary<string, string>()
    {
        ["key3"] = "value3",
        ["key4"] = "value4"
    };
    
    fields.Items = new Dictionary<string, Dictionary<string, string>>()
    {
        ["name1"] = item1,
        ["name2"] = item2,
    };
    
    Login or Signup to reply.
  2. Json that you want is not valid if name1 can be equal to name2. So it is impossible to create the json you want. You can create it manually, using a StringBuilder for example, but you will not be able to deserialize it and get data (if you don’t create your own json library with your own rules). The only structure that allows duplicates is a collection ( an array or a list for example). So discuss it with the users of API and update your post. We can offer dozens of different structures for you that are valid, but I can not see any sense in doing it. You can read any textbook instead.

    IMHO the dictionaries are not the best structures for data models. It is always better to use fully typed objects instead of strings. You have to use dictionaries only if you can’t use anything else.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search