A way to combine 2 unrelated JSON objects












0















Suppose I have 2 objects:



public class CInfo
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }
}

public class Event
{
[JsonProperty(PropertyName = "Time")]
public DateTime dateTime { get; set; }

[JsonProperty(PropertyName = "Note")]
public string comment { get; set; }
}


Couple of functions return the following:



CInfo cInfo = getCInfo();
List<Event> = getEvents();


Both of these meant to be combined together, into a final (hypothetical) object that needs to look this:



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


At which point the intention is to do a JsonConvert.SerialzeObject(CInfoWithEvents), and get a json string representing this final object.



Question: what is the best way to combine them? There has to be a more elegant way than just creating whole new object and copying each property, or worse: serialize cInfo and List<Event> separately and do some sort of string manipulation.



Thank you for your time










share|improve this question























  • Include the entire CInfo as a single property? class CInfoWithEvents { public CInfo CInfo {get;set;}; public List<Event> {get;set;}} Or do something like stackoverflow.com/q/42836936/11683?

    – GSerg
    Dec 28 '18 at 20:02













  • @GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a CInfo with List<Event> property added. I considered creating a derived class class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List<Event> { get; set; }} but i'd be back to just copying properties one by one if I wanted to populate this new object.

    – concentriq
    Dec 28 '18 at 20:14


















0















Suppose I have 2 objects:



public class CInfo
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }
}

public class Event
{
[JsonProperty(PropertyName = "Time")]
public DateTime dateTime { get; set; }

[JsonProperty(PropertyName = "Note")]
public string comment { get; set; }
}


Couple of functions return the following:



CInfo cInfo = getCInfo();
List<Event> = getEvents();


Both of these meant to be combined together, into a final (hypothetical) object that needs to look this:



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


At which point the intention is to do a JsonConvert.SerialzeObject(CInfoWithEvents), and get a json string representing this final object.



Question: what is the best way to combine them? There has to be a more elegant way than just creating whole new object and copying each property, or worse: serialize cInfo and List<Event> separately and do some sort of string manipulation.



Thank you for your time










share|improve this question























  • Include the entire CInfo as a single property? class CInfoWithEvents { public CInfo CInfo {get;set;}; public List<Event> {get;set;}} Or do something like stackoverflow.com/q/42836936/11683?

    – GSerg
    Dec 28 '18 at 20:02













  • @GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a CInfo with List<Event> property added. I considered creating a derived class class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List<Event> { get; set; }} but i'd be back to just copying properties one by one if I wanted to populate this new object.

    – concentriq
    Dec 28 '18 at 20:14
















0












0








0








Suppose I have 2 objects:



public class CInfo
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }
}

public class Event
{
[JsonProperty(PropertyName = "Time")]
public DateTime dateTime { get; set; }

[JsonProperty(PropertyName = "Note")]
public string comment { get; set; }
}


Couple of functions return the following:



CInfo cInfo = getCInfo();
List<Event> = getEvents();


Both of these meant to be combined together, into a final (hypothetical) object that needs to look this:



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


At which point the intention is to do a JsonConvert.SerialzeObject(CInfoWithEvents), and get a json string representing this final object.



Question: what is the best way to combine them? There has to be a more elegant way than just creating whole new object and copying each property, or worse: serialize cInfo and List<Event> separately and do some sort of string manipulation.



Thank you for your time










share|improve this question














Suppose I have 2 objects:



public class CInfo
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }
}

public class Event
{
[JsonProperty(PropertyName = "Time")]
public DateTime dateTime { get; set; }

[JsonProperty(PropertyName = "Note")]
public string comment { get; set; }
}


Couple of functions return the following:



CInfo cInfo = getCInfo();
List<Event> = getEvents();


Both of these meant to be combined together, into a final (hypothetical) object that needs to look this:



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "Id")]
public string cID { get; set; }

[JsonProperty(PropertyName = "Name")]
public string cName { get; set; }

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


At which point the intention is to do a JsonConvert.SerialzeObject(CInfoWithEvents), and get a json string representing this final object.



Question: what is the best way to combine them? There has to be a more elegant way than just creating whole new object and copying each property, or worse: serialize cInfo and List<Event> separately and do some sort of string manipulation.



Thank you for your time







c# json object serialization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 19:58









concentriqconcentriq

50311




50311













  • Include the entire CInfo as a single property? class CInfoWithEvents { public CInfo CInfo {get;set;}; public List<Event> {get;set;}} Or do something like stackoverflow.com/q/42836936/11683?

    – GSerg
    Dec 28 '18 at 20:02













  • @GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a CInfo with List<Event> property added. I considered creating a derived class class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List<Event> { get; set; }} but i'd be back to just copying properties one by one if I wanted to populate this new object.

    – concentriq
    Dec 28 '18 at 20:14





















  • Include the entire CInfo as a single property? class CInfoWithEvents { public CInfo CInfo {get;set;}; public List<Event> {get;set;}} Or do something like stackoverflow.com/q/42836936/11683?

    – GSerg
    Dec 28 '18 at 20:02













  • @GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a CInfo with List<Event> property added. I considered creating a derived class class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List<Event> { get; set; }} but i'd be back to just copying properties one by one if I wanted to populate this new object.

    – concentriq
    Dec 28 '18 at 20:14



















Include the entire CInfo as a single property? class CInfoWithEvents { public CInfo CInfo {get;set;}; public List<Event> {get;set;}} Or do something like stackoverflow.com/q/42836936/11683?

– GSerg
Dec 28 '18 at 20:02







Include the entire CInfo as a single property? class CInfoWithEvents { public CInfo CInfo {get;set;}; public List<Event> {get;set;}} Or do something like stackoverflow.com/q/42836936/11683?

– GSerg
Dec 28 '18 at 20:02















@GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a CInfo with List<Event> property added. I considered creating a derived class class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List<Event> { get; set; }} but i'd be back to just copying properties one by one if I wanted to populate this new object.

– concentriq
Dec 28 '18 at 20:14







@GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a CInfo with List<Event> property added. I considered creating a derived class class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List<Event> { get; set; }} but i'd be back to just copying properties one by one if I wanted to populate this new object.

– concentriq
Dec 28 '18 at 20:14














1 Answer
1






active

oldest

votes


















1














you can implement this in various ways besides copying each property of both classes:



1) Add both class as public property in the third class.



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "CInfo")]
public CInfo {get; set;}

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


2) Inherit from CInfo class and have a List as public property.



public class CInfoWithEvents : CInfo
{
[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


3) Create on the fly:



  var javaScriptSerializer = new JavaScriptSerializer();
var resultJson = javaScriptSerializer.Serialize(new { CInfo = getCInfo(), Events = getEvents()});


4) Using JObject



    JObject jCInfo = JObject.FromObject(getCInfo());
jCInfo.Add("Events", JArray.FromObject(getEvents()));
string json = jCInfo.ToString();





share|improve this answer


























  • Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

    – concentriq
    Dec 28 '18 at 20:52






  • 1





    please check #4

    – Sonal Borkar
    Dec 28 '18 at 21:08











  • this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

    – concentriq
    Dec 28 '18 at 21:28













  • Welcome, Edited my answer now. Enjoy Coding! !!

    – Sonal Borkar
    Dec 28 '18 at 21:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963715%2fa-way-to-combine-2-unrelated-json-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














you can implement this in various ways besides copying each property of both classes:



1) Add both class as public property in the third class.



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "CInfo")]
public CInfo {get; set;}

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


2) Inherit from CInfo class and have a List as public property.



public class CInfoWithEvents : CInfo
{
[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


3) Create on the fly:



  var javaScriptSerializer = new JavaScriptSerializer();
var resultJson = javaScriptSerializer.Serialize(new { CInfo = getCInfo(), Events = getEvents()});


4) Using JObject



    JObject jCInfo = JObject.FromObject(getCInfo());
jCInfo.Add("Events", JArray.FromObject(getEvents()));
string json = jCInfo.ToString();





share|improve this answer


























  • Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

    – concentriq
    Dec 28 '18 at 20:52






  • 1





    please check #4

    – Sonal Borkar
    Dec 28 '18 at 21:08











  • this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

    – concentriq
    Dec 28 '18 at 21:28













  • Welcome, Edited my answer now. Enjoy Coding! !!

    – Sonal Borkar
    Dec 28 '18 at 21:53
















1














you can implement this in various ways besides copying each property of both classes:



1) Add both class as public property in the third class.



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "CInfo")]
public CInfo {get; set;}

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


2) Inherit from CInfo class and have a List as public property.



public class CInfoWithEvents : CInfo
{
[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


3) Create on the fly:



  var javaScriptSerializer = new JavaScriptSerializer();
var resultJson = javaScriptSerializer.Serialize(new { CInfo = getCInfo(), Events = getEvents()});


4) Using JObject



    JObject jCInfo = JObject.FromObject(getCInfo());
jCInfo.Add("Events", JArray.FromObject(getEvents()));
string json = jCInfo.ToString();





share|improve this answer


























  • Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

    – concentriq
    Dec 28 '18 at 20:52






  • 1





    please check #4

    – Sonal Borkar
    Dec 28 '18 at 21:08











  • this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

    – concentriq
    Dec 28 '18 at 21:28













  • Welcome, Edited my answer now. Enjoy Coding! !!

    – Sonal Borkar
    Dec 28 '18 at 21:53














1












1








1







you can implement this in various ways besides copying each property of both classes:



1) Add both class as public property in the third class.



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "CInfo")]
public CInfo {get; set;}

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


2) Inherit from CInfo class and have a List as public property.



public class CInfoWithEvents : CInfo
{
[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


3) Create on the fly:



  var javaScriptSerializer = new JavaScriptSerializer();
var resultJson = javaScriptSerializer.Serialize(new { CInfo = getCInfo(), Events = getEvents()});


4) Using JObject



    JObject jCInfo = JObject.FromObject(getCInfo());
jCInfo.Add("Events", JArray.FromObject(getEvents()));
string json = jCInfo.ToString();





share|improve this answer















you can implement this in various ways besides copying each property of both classes:



1) Add both class as public property in the third class.



public class CInfoWithEvents
{
[JsonProperty(PropertyName = "CInfo")]
public CInfo {get; set;}

[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


2) Inherit from CInfo class and have a List as public property.



public class CInfoWithEvents : CInfo
{
[JsonProperty(PropertyName = "Events")]
public List<Event> { get; set; }
}


3) Create on the fly:



  var javaScriptSerializer = new JavaScriptSerializer();
var resultJson = javaScriptSerializer.Serialize(new { CInfo = getCInfo(), Events = getEvents()});


4) Using JObject



    JObject jCInfo = JObject.FromObject(getCInfo());
jCInfo.Add("Events", JArray.FromObject(getEvents()));
string json = jCInfo.ToString();






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 21:52

























answered Dec 28 '18 at 20:22









Sonal BorkarSonal Borkar

510211




510211













  • Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

    – concentriq
    Dec 28 '18 at 20:52






  • 1





    please check #4

    – Sonal Borkar
    Dec 28 '18 at 21:08











  • this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

    – concentriq
    Dec 28 '18 at 21:28













  • Welcome, Edited my answer now. Enjoy Coding! !!

    – Sonal Borkar
    Dec 28 '18 at 21:53



















  • Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

    – concentriq
    Dec 28 '18 at 20:52






  • 1





    please check #4

    – Sonal Borkar
    Dec 28 '18 at 21:08











  • this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

    – concentriq
    Dec 28 '18 at 21:28













  • Welcome, Edited my answer now. Enjoy Coding! !!

    – Sonal Borkar
    Dec 28 '18 at 21:53

















Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

– concentriq
Dec 28 '18 at 20:52





Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to CInfoWithEvents object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance.

– concentriq
Dec 28 '18 at 20:52




1




1





please check #4

– Sonal Borkar
Dec 28 '18 at 21:08





please check #4

– Sonal Borkar
Dec 28 '18 at 21:08













this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

– concentriq
Dec 28 '18 at 21:28







this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution

– concentriq
Dec 28 '18 at 21:28















Welcome, Edited my answer now. Enjoy Coding! !!

– Sonal Borkar
Dec 28 '18 at 21:53





Welcome, Edited my answer now. Enjoy Coding! !!

– Sonal Borkar
Dec 28 '18 at 21:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963715%2fa-way-to-combine-2-unrelated-json-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas