JakeMangan
JakeMangan

Reputation: 49

Is there a less cumbersome way to generate large repetitive JSON files in C#?

My end goal is to produce a JSON file that has the following structure

{
  "Info": [
    {
      "Detail1": "value1",
      "Detail2": "value2",
      "Detail3": "value3",
      "Fields": [
        {
          "Description": "Field1",
          "Value": "value4"
        },
        {
          "Description": "Field2",
          "Value": "value5"
        },
        {
          "Description": "Field3",
          "Value": "value6"
        }
        ,
        {
        ... continue for another 100~ fields
        },

      ]
    }
  ]
}

This JSON file contains a number of header details, denoted by Detail1, Detail2 and Detail3. It then contains an array Fields that contains around 100 separate elements, each field contains a unique description and value, all of which are known prior. The values for the Fields elements are stored in a separate class.

Please note that Detail1 and value1 etc are just placeholder names, the actual data being used is different.

At the moment the code I have to generate this JSON is extremely long, ugly and hardcoded and just isn't an elegant solution. I'd much rather replace the code with something else if there is any other solution available.

Here is the code I have at the moment

JObject result = new JObject(
                new JProperty("Info",
                    //new JObject(
                    new JArray(
                        new JObject(
                            new JProperty("Detail1", preJson.value1),
                            new JProperty("Detail2", preJson.value2),
                            new JProperty("Detail3", preJson.value3),
                            new JProperty("Fields",
                                new JArray(
                                    new JObject(
                                        new JProperty("Description", "Field1"),
                                        new JProperty("Value", preJson.Field1)),
                                    new JObject(
                                        new JProperty("Description", "Field2"),
                                        new JProperty("Value", preJson.Field2)),
                                    new JObject(
                                        new JProperty("Description", "Field3"),
                                        new JProperty("Value", preJson.Field3)),
                                    new JObject(
                                        ...continue for 100~ fields
                               )))))));

preJson is simply a DTO object that contains all of the information required within the JSON file and is used to organise the data before restructuring into JSON format.

The only solution to this I can think of is including the descriptions for each element within Fields within a text file or array and then somehow iterating through the array and extracting the details of each element in turn, instead of hard coding each individual element within a new JObject, but I am unsure whether this would even work.

Upvotes: 0

Views: 270

Answers (1)

Tarik Tutuncu
Tarik Tutuncu

Reputation: 818

You can generate the classes for your model like:

public class Field
{
    public string Description { get; set; }
    public string Value { get; set; }
}

public class Info
{
    public string Detail1 { get; set; }
    public string Detail2 { get; set; }
    public string Detail3 { get; set; }
    public List<Field> Fields { get; set; }
}

public class RootObject
{
    public List<Info> Info { get; set; }
}

Next populate the data and then assuming you are using Newtonsoft JSON.Net you can serialize the RootObject like:

var output = JsonConvert.SerializeObject(product);

Upvotes: 3

Related Questions