Matthew Pigram
Matthew Pigram

Reputation: 1430

Newtonsoft.JSON Ignore parent property but serialize child properties

I have the following json data structure that I am trying to serialize, as is shown there are guids used for some of the fields. Ideally I would like to ignore these during serialization but I still want to be able to serialize the child properties of these objects (published_show_time etc). I would want to create these in an array since each record is a separate entry. Is there a way to do this using the Newtonsoft JSON library or will I need to create a custom Json converter?

{
    "data": {
        "019765d6-9e37-474c-b53b-ddb24c8c5fc8": {
            "published_show_time": null,
            "preshow_duration": 1131.5033333333,
            "content_issue": false,
            "has_intermission": false,
        },
        "6ac935d2-bca8-4c4d-8c39-2cb9f17ee0be": {
            "published_show_time": null,
            "preshow_duration": 10.0,
            "content_issue": false,
            "has_intermission": false,
        }
    }
}

Upvotes: 1

Views: 1398

Answers (2)

Winter
Winter

Reputation: 191

You can serialize the Json as Dictionary struct

class MyJson
{
    [JsonProperty("data")]
    public Dictionary<string,MyData> Data {get;set;}
}

class MyData
{
   [JsonProperty("published_show_time")]
   public string  PublishedShowTime {get;set;}
   ......//preshow_duration..and more
}

Then you can serialize this class like:

 var data =JsonConvert.SerializeObject(new MyJson(){ ... });

Upvotes: 1

Krusty
Krusty

Reputation: 1183

The only solution that comes to my mind is to use a converter. You can use a LINQ to JSON as well, but I wouldn't advice for it

Upvotes: 0

Related Questions