2

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.

James Studer
  • 37
  • 1
  • 6
  • It sounds like you are trying to deserialize JSON and create a new object. Take a look at this question https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp – Leonardo Wildt Nov 13 '17 at 21:38

1 Answers1

0

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.

Brandon Miller
  • 1,534
  • 1
  • 11
  • 16
  • in this case there can be more then one data section. That is specified in the meta section. So I need a list data model. Each data model has org data first that is about 40 attribuates. Those 40 attrubates are not always sent. We could send 1, 5, 20 or 40. I loaded it into a list and found after that, that the API didn't like having the org tag before it with the [] included. As for address, it needs to be a list because an org can have more then one address and this is how the receiving api wants it structured. – James Studer Nov 13 '17 at 21:34
  • Okay, you could possibly create your DataModel to hold a list of a new class, say OrgClass that holds a single EntityProperties object for the actual org and a List for the addresses the org contains? The problem with your data model is that you have 2 lists inside of your original Dataset, which will produce 2 JSON arrays within the original (data) array. Which, from what I read, is not what we want here. If you want a single array of objects, you're going to have to have DataModel contain only a single object reference. For every List, JSON.Net creates a new array. – Brandon Miller Nov 13 '17 at 21:41
  • That being said, I would just scrap creating the OrgClass and make datamodel hold a single object for Org and List of addresses. – Brandon Miller Nov 13 '17 at 21:42
  • I did as you said and now it is just EntityProperties and that have removed the [] from it. I am now getting an invalid message 400 bad request from the api. This appears to be caused by "org" with {} still present. Almost there. – James Studer Nov 13 '17 at 22:07
  • Could you possibly update your question with the new JSON format? – Brandon Miller Nov 14 '17 at 13:27