I need to join the deserialized data from multiple JSON files into a single file.
I am requesting energy data from an open API that returns one or more JSON files. After deserializing each file, I need to join the deserialized data together to form a single file.
My current code to deserialize a single file is:
myOctopusDeserializeData =
JsonConvert.DeserializeObject<Rootobject>(
new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd());
where Rootobject
is defined as:
public class Rootobject
{
public int count { get; set; }
public string next { get; set; }
public object previous { get; set; }
public Result[] results { get; set; }
}
public class Result
{
public double consumption { get; set; }
public DateTime interval_start { get; set; }
public DateTime interval_end { get; set; }
}
Mock data example:
File 1 returned:
{
"count": 3,
"next": "https://api.octopus.energy/v1/gas-meter-points/30831.....",
"previous": null,
"results": [
{
"consumption": 0.0,
"interval_start": "2023-06-20T01:00:00+01:00",
"interval_end": "2023-06-20T01:30:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-20T01:30:00+01:00",
"interval_end": "2023-06-20T02:00:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-20T02:00:00+01:00",
"interval_end": "2023-06-20T02:30:00+01:00"
},
]
}
File 2 returned:
{
"count": 3,
"next": "https://api.octopus.energy/v1/gas-meter-points/30842.....",
"previous": "https://api.octopus.energy/v1/gas-meter-points/30831.....",
"results": [
{
"consumption": 0.0,
"interval_start": "2023-06-21T23:30:00+01:00",
"interval_end": "2023-06-22T00:00:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-22T00:00:00+01:00",
"interval_end": "2023-06-22T00:30:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-22T00:30:00+01:00",
"interval_end": "2023-06-22T01:00:00+01:00"
},
]
}
Desired output:
{
"count": 6,
"next": null,
"previous": null,
"results": [
{
"consumption": 0.0,
"interval_start": "2023-06-20T01:00:00+01:00",
"interval_end": "2023-06-20T01:30:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-20T01:30:00+01:00",
"interval_end": "2023-06-20T02:00:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-20T02:00:00+01:00",
"interval_end": "2023-06-20T02:30:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-21T23:30:00+01:00",
"interval_end": "2023-06-22T00:00:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-22T00:00:00+01:00",
"interval_end": "2023-06-22T00:30:00+01:00"
},
{
"consumption": 0.0,
"interval_start": "2023-06-22T00:30:00+01:00",
"interval_end": "2023-06-22T01:00:00+01:00"
},
]
}
I need to join the deserialized data from multiple JSON files into a single file.