1

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?

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
user3884423
  • 556
  • 1
  • 5
  • 20
  • 1
    Shouldn't `"count"` be `2` in each of `1.json` and `2.json` (while remaining `4` in the combined file)? – Flydog57 Jul 04 '23 at 20:55
  • Consider creating two classes, one that represents the _header_ (count, next, previous and a collection of results), and one that represents an individual result. Deserialize each file into a separate object. Then merge the results into a third one and serialize it back out – Flydog57 Jul 04 '23 at 20:59
  • Use `JContainer.Merge()` with `MergeArrayHandling.Concat` as shown in [this answer](https://stackoverflow.com/a/27502360/3744182) to [Merge two Json.NET arrays by concatenating contained elements](https://stackoverflow.com/q/14121010/3744182) and [this answer](https://stackoverflow.com/a/37756929/3744182) to [Merge two JTokens into one](https://stackoverflow.com/q/37756656/3744182). In fact your question looks like a duplicate, agree? – dbc Jul 04 '23 at 21:47
  • @Flydog57: Unfortunately the API sends me the total number of items each time e.g. 4. I have to read how many I have got to know I need to make another call or not. The API is not mine so I have to work with what I get from it. You are right and your method would be better if I had the number in each file. – user3884423 Jul 05 '23 at 15:58

1 Answers1

1

just add items to JArray

    o1["count"] = ((JArray)o1["results"]).Count
                     + ((JArray)o2["results"]).Count;

    foreach (JObject  o in o2["results"])
         ((JArray)o1["results"]).Add(o);
    
    string json = o1.ToString();
Serge
  • 40,935
  • 4
  • 18
  • 45