0

I have this JsonDocument which looks like this

string jsonString = "{\"year\": 0, \"class\": [], \"schools\": []}";
JsonDocument newSchema = JsonDocument.Parse(jsonString)

And a bunch of JSON files, with these properties filed out. Each of the JSON file have the same year, but contain information of one specific class and one specific school. None of the them is the same beside the year.

I trying to create a one JsonDocument with all class and schools put together into one JsonDocument, but seem to have a problem with appending the JsonProperty together..

This is what I have tried so far:

using (JsonDocument newSchema = JsonDocument.Parse(jsonString))
{
    List<JsonProperty> properties = new List<JsonProperty>();
    foreach(JsonProperty a in newSchema.RootElement.EnumerateObject())
    {
        properties.Add(a);
    }
    foreach (string classandSchoolDefinition in loadableSchemaDefinitions)
    {

        string schemaDefinitionAsJson = File.ReadAllText(classandSchoolDefinition );
        JsonDocument schemaJson = JsonDocument.Parse(schemaDefinitionAsJson);

        foreach (JsonProperty a in schemaJson.RootElement.EnumerateObject())
        {
            switch (a.Name)
            {
                case "year":
                    Console.WriteLine($@"({a.Name}+{a.Value}, {a.Value.ValueKind})");
                    break;
                case "class":
                    Console.WriteLine($@"({a.Name}+{a.Value}, {a.Value.ValueKind})");
                    break;
                case "school":
                    Console.WriteLine($@"({a.Name}+{a.Value}, {a.Value.ValueKind})");
                    break;
                default:
                    break;
            }
        }
    }

How do I concat each the Jsonproperty value to one combined newSchema, and not keep the into one?
In this case I only want append an array to another array.

I am not Fat
  • 283
  • 11
  • 36
  • *I trying to create a one JsonDocument with all class and schools put together into one JsonDocument,* -- can you explain how you want to do this? It's not clear from your question what the correct merging algorithm should be. Maybe you could [edit] your question to include a couple of example files, and the desired result -- i.e. a [mcve]? – dbc Feb 05 '21 at 13:46
  • @dbc - `newSchema` contains no classes and schools, but each of json files contains an EnumerableObject within the EnumerableArray fields class and school, Each of those EnumberableObject which can be found in the json files, has to be added to the EnumerableArray. – I am not Fat Feb 05 '21 at 14:48
  • I need an AddEnumerableObjectToEnumerableArray(array, object) function – I am not Fat Feb 05 '21 at 14:50
  • Could you share a couple of simple JSON examples as strings, showing your inputs and desired output? Do you just want to concatenate the `"class"` and `"schools"` arrays? What should happen if the `"year"` values differ? – dbc Feb 05 '21 at 14:50
  • I am not interested in `year` atm, but if they are different it should fail. – I am not Fat Feb 05 '21 at 14:51
  • And yes, basically a concatenation, adding a new object to the array. – I am not Fat Feb 05 '21 at 14:52

1 Answers1

1

You can reach this with Newtonsoft.Json

For example, you have

var o1 = JObject.Parse(@"{
  'Name': 'Max',
  'Enabled': false,
  'Roles': [ 'User' ]
}");

and another object

var o2 = JObject.Parse(@"{
  'Enabled': true,
  'Roles': [ 'User', 'Admin' ]
}");

simply use Merge function like this -

o1.Merge(o2, new JsonMergeSettings
{
    // to avoid duplicates
    MergeArrayHandling = MergeArrayHandling.Union
});

the result will be -

{
   "FirstName": "Max",
   "Enabled": true,
   "Roles": [
      "User",
      "Admin"
  ]
}
  • I have too many dependency to `system.text.json` hence cannot use the `Newtonsoft.Json` – I am not Fat Feb 05 '21 at 13:28
  • 1
    System.Text.Json does not have a mechanism to merge jsons because the existing JsonDocument, JsonElement APIs are read-only https://github.com/dotnet/runtime/issues/31433 Take a look at this answer, may it will help https://stackoverflow.com/a/59574030/7436855 – Maxim Vorchakov Feb 05 '21 at 13:37
  • I ended up using the solution mentioned in the github issue. – I am not Fat Feb 10 '21 at 11:24