C# JSON.Net place items in one list into another

103 views Asked by At

I have a JSON that I am creating in C# using JSON.net. My object contains a meta and data sections. The data section is a JSON Array and contains other JSON Array's in it. The problem I have is my main data entity. Right now I have all the data for that entity written in a list. The problem is I need to extract all that data from the list and move it up to the data level. Here is what I am outputting right now:

{
   "meta":
   {
      //meta info here.  This is static and formatted correctly.
   }
   "data":
   [
      {
        "main record data:"
        [
          {
            //Here is dynamically created data that I need to move.
          }
        ]
      }
   ]
}

My object in C# has the main class which defines the meta and data sections of my JSON. The data section is a List<DataModel>. Within that are all my other lists to setup each section that is included in the data section of the JSON. The list I need is an organization list. Here is the the condensed model:

public class JSONModel 
    {
        [JsonProperty(Order = 1)]
        public EntityProperties meta { get; set; }
        [JsonProperty(Order = 2)]
        public List<DataModel> data { get; set; }
    }
    public class DataModel
    {
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<EntityProperties> org { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<EntityProperties> addresses { get; set; }
    }

What I need to output is this:

{
   "meta":
   {
      //meta info here.  This is static and formatted correctly.
   }
   "data":
   [
      {
        //Here is dynamically created data from the org list.
      }
   ]
}

The tool I am using is SCRIBE Online and this is a custom connector I am building. That is where the <EntityProperties> comes from. That is what they provide to me and then I just pass them into my list and it puts them into the proper JSON format with label: data. If the org entity was going to have static fields like the meta, then it would be simple in my opionion. I am hoping that I can just pull the data into a JObject and then insert them back in at the top of my data section, but this is my first go with JSON.net and I am not sure if I am on the right path. Any input would be greatly appreciated.

1

There are 1 answers

5
Brandon Miller On

Instead of having a List<DataModel> you should just have a single object of DataModel.

If you want to organize your "org" and "address" into single object instances then create a class that holds a reference to a single object of each, and then create a list of that class in your DataModel class.