Home > Blockchain >  How do I create a model from a JSON Key in C#?
How do I create a model from a JSON Key in C#?

Time:06-09

I have this JSON

{
"07-2022": [],
"08-2022": [
    {
        "event_title": "asdf",
        "positions": [
            {
                "position_title": "Graphics Op"
            },
            {
                "position_title": "Graphic Design"
            }
        ]
    }
],
"06-2023": []
}

it is a "month" key whose value is an array of objects that I call Job. Each job has an array of position objects.

so I can make those two models easily

public class Job {
    public string event_title {get;set;}
    public List<Position> positions {get; set;}
}

public class Position {
    public string position_title {get; set;}
}

How do i get the date string into a month class that contains a List of Jobs?

public class EventMonth {
    public string month {get; set;}
    public List<Job> jobs {get; set;}
}

CodePudding user response:

Not an answer, especially as it's likely you have no control over this. But in case you do, this a note to suggest the JSON is poorly structured (and fixing it could make your mapping much easier!)

You have this:

{
"07-2022": [],
"08-2022": [
    {
...

This is an object with properties named after month keys. Each property entry then includes an array of objects.

A better way to structure this JSON is as a dictionary, more like this:

[
 {
    "monthKey":"2022-07-01",
    "events":[]
 },
 {
    "monthKey":"2022-08-01",
    "events":[
      {
         ...

That is, we start with an array, since that is really what the entries are. Each item in the array is an object with a property monthKey, to tell us about the items. This key is built in a way that will make it easier to create a full date object representing the first date of the month. The array objectys also have an "events" array.

In this way, we more accurately communicate the meta data about the events, and we do it in a way that makes it easier for languages that don't allow property names with only digits to model.

As an additional note, nowhere in the sample is there ever more than one event in the month. This is likely incidental to the sample data, but if not we should also consider changing the events property into a object, instead of an array.

CodePudding user response:

Since your json is not type of List<EventMonth> but Dictionary<string, List<Job>> you need to implement model converting:

...

using Newtonsoft.Json;

...

public class Job
{
    [JsonProperty("event_title")]
    public string EventTitle {get;set;}
    
    [JsonProperty("positions")]
    public List<Position> Positions { get; set; }
}
public class Position 
{
    [JsonProperty("position_title")]
    public string PositionTitle { get; set; }
}
public class EventMonth 
{
    [JsonProperty("month")]
    public string Month { get; set; }
    
    [JsonProperty("jobs")]
    public List<Job> Jobs { get; set; }
}

...

var subResult = JsonConvert.DeserializeObject<Dictionary<string, List<Job>>>(jsonFromQuestion);
var result = subResult.Select(s => new EventMonth
{
    Month = s.Key,
    Jobs = s.Value
}).ToList();

EDIT: Same code but with using System.Text.Json

...

using System.Text.Json;    
using System.Text.Json.Serialization;

...

public class Job
{
    [JsonPropertyName("event_title")]
    public string EventTitle {get;set;}
    
    [JsonPropertyName("positions")]
    public List<Position> Positions { get; set; }
}
public class Position 
{
    [JsonPropertyName("position_title")]
    public string PositionTitle { get; set; }
}
public class EventMonth 
{
    [JsonPropertyName("month")]
    public string Month { get; set; }
    
    [JsonPropertyName("jobs")]
    public List<Job> Jobs { get; set; }
}

...

var subResult = JsonSerializer.Deserialize<Dictionary<string, List<Job>>>(jsonFromQuestion);
var result = subResult.Select(s => new EventMonth
{
    Month = s.Key,
    Jobs = s.Value
}).ToList();
  • Related