I am trying to merge two files but they will not merge. I step through my code, the code I use to merge the files is below, each line runs but I do not get file 2 merged into file 1. The files are:
File = 1.json
{
"count": 4,
"next": null,
"previous": null,
"results": [
{
"consumption": 0.004,
"interval_start": "2023-07-01T07:00:00+01:00",
"interval_end": "2023-07-01T07:30:00+01:00"
},
{
"consumption": 0.007,
"interval_start": "2023-07-01T07:30:00+01:00",
"interval_end": "2023-07-01T08:00:00+01:00"
}
]
}
File = 2.json
{
"count": 4,
"next": null,
"previous": null,
"results": [
{
"consumption": 0.007,
"interval_start": "2023-07-01T08:00:00+01:00",
"interval_end": "2023-07-01T08:30:00+01:00"
},
{
"consumption": 0.005,
"interval_start": "2023-07-01T08:30:00+01:00",
"interval_end": "2023-07-01T09:00:00+01:00"
}
]
}
I am expecting to get 1.json as the resultant merged file as below
{
"count": 4,
"next": null,
"previous": null,
"results": [
{
"consumption": 0.004,
"interval_start": "2023-07-01T07:00:00+01:00",
"interval_end": "2023-07-01T07:30:00+01:00"
},
{
"consumption": 0.007,
"interval_start": "2023-07-01T07:30:00+01:00",
"interval_end": "2023-07-01T08:00:00+01:00"
},
{
"consumption": 0.007,
"interval_start": "2023-07-01T08:00:00+01:00",
"interval_end": "2023-07-01T08:30:00+01:00"
},
{
"consumption": 0.005,
"interval_start": "2023-07-01T08:30:00+01:00",
"interval_end": "2023-07-01T09:00:00+01:00"
}
]
}
The piece of code I use to merge the two files is
JObject o1 = JObject.Parse(Utilities.LoadJson(myPath)); //myPath = 1.json
JObject o2 = JObject.Parse(Utilities.LoadJson(path)); //path = 2.json
o1.Merge(o2);
//I also tried this line below instead of the line above but seems to result in the same
// o1.Merge(o2, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat });
The function LoadJson() reads the file into a string as I believe the JOject needs to be parsed from a string
public static string LoadJson(string myFile)
{
using (StreamReader r = new StreamReader(myFile))
{
return r.ReadToEnd();
}
}
How can I get this to work the way I need it to work?