2

I got a json as input. I don't know exact structure of that file. I need only add object to "KnownCollection" which may be in file or not. So if there is a collection add item into it, if there is not create a collection with an item. Question is how to do that with System.Text.Json, we used it and I don't want to add dependency on Newtonsoft

{
    "UnknownProperty": 1,
    "KnownCollection": [{
      "ItemProperty1": "1",
      "ItemProperty2": 2
    }],
    // More unknown properties
  }
var item = new {ItemProperty1 = "2", ItemProperty2 = 2};
var fileContent = File.ReadAllBytes(filePath);
using var jsonDocument = JsonDocument.Parse(fileContent);
var knownCollection = jsonDocument.RootElement.GetProperty("KnownCollection");

//How to add item to knownCollection?
Rena
  • 30,832
  • 6
  • 37
  • 72
Peter Pollen
  • 83
  • 1
  • 6
  • Something like this? [https://stackoverflow.com/questions/33081102/json-add-new-object-to-existing-json-file-c-sharp/33081258](https://stackoverflow.com/questions/33081102/json-add-new-object-to-existing-json-file-c-sharp/33081258) – mamen Mar 24 '21 at 06:31
  • 1
    @mamen yes, exactly! But I need to use System.Text.Json in that sample is used newtonsoft json – Peter Pollen Mar 24 '21 at 07:06
  • Option #2 from [this answer](https://stackoverflow.com/a/59001666/3744182) to [Modifying a JSON file using System.Text.Json](https://stackoverflow.com/q/58997718/3744182) should work here. – dbc Mar 24 '21 at 13:15

1 Answers1

2

You could add the item to array like below:

[HttpGet]
public string Index()
{
    var fileContent = System.IO.File.ReadAllBytes("filePath");
    using (MemoryStream memoryStream1 = new MemoryStream())
    {
        using (Utf8JsonWriter utf8JsonWriter1 = new Utf8JsonWriter(memoryStream1))
        {
            using (JsonDocument jsonDocument = JsonDocument.Parse(fileContent))
            {
                utf8JsonWriter1.WriteStartObject();

                foreach (var element in jsonDocument.RootElement.EnumerateObject())
                {
                    if (element.Name == "KnownCollection")
                    {
                        utf8JsonWriter1.WritePropertyName(element.Name);
                        utf8JsonWriter1.WriteStartArray();
                        // Staring new object
                        utf8JsonWriter1.WriteStartObject();

                            utf8JsonWriter1.WriteString("ItemProperty1", "2");
                            utf8JsonWriter1.WriteNumber("ItemProperty2", 2);
                            utf8JsonWriter1.WriteEndObject();

                            // Copying existing values from "KnownCollection" array
                            foreach (var testDataElement in element.Value.EnumerateArray())
                            {
                                testDataElement.WriteTo(utf8JsonWriter1);
                            }

                            utf8JsonWriter1.WriteEndArray();
                    }
                    else
                    {
                        element.WriteTo(utf8JsonWriter1);
                    }
                }

                utf8JsonWriter1.WriteEndObject();
            }
         }

        var resultJson = Encoding.UTF8.GetString(memoryStream1.ToArray());
        return resultJson;
    }

}

Result:

enter image description here

Rena
  • 30,832
  • 6
  • 37
  • 72