Modifying a JSON file using System.Text.Json Modifying a JSON file using System.Text.Json json json

Modifying a JSON file using System.Text.Json


Your problem is that you would like to retrieve, filter, and pass along some JSON without needing to define a complete data model for that JSON. With Json.NET, you could use LINQ to JSON for this purpose. Your question is, can this currently be solved as easily with System.Text.Json?

As of .NET Core 3.0, this cannot be done quite as easily with System.Text.Json because:

  1. JsonDocument, the type corresponding to JToken or XDocument, is read-only. It can be used only to examine JSON values, not to modify or create JSON values.

    There is currently an open issue Writable Json DOM #39922 tracking this.

  2. System.Text.Json has no support for JSONPath which is often quite convenient in such applications.

    There is currently an open issue Add JsonPath support to JsonDocument/JsonElement #41537 tracking this.

That being said, imagine you have the following JSON:

[  {    "id": 1,    "name": "name 1",    "address": {      "Line1": "line 1",      "Line2": "line 2"    },    // More properties omitted  }  //, Other array entries omitted]

And some Predicate<long> shouldSkip filter method indicating whether an entry with a specific id should not be returned, corresponding to CHECKS in your question. What are your options?

You could use JsonDocument and return some filtered set of JsonElement nodes. This makes sense if the filtering logic is very simple and you don't need to modify the JSON in any other way. Be aware that JsonDocument is disposable, and in fact must needs be disposed to minimize the impact of the garbage collector (GC) in high-usage scenarios, according to the docs. Thus, in order to return a JsonElement you must clone it.

The following code shows an example of this:

using var usersDocument = JsonDocument.Parse(rawJsonDownload);var users = usersDocument.RootElement.EnumerateArray()    .Where(e => !shouldSkip(e.GetProperty("id").GetInt64()))    .Select(e => e.Clone())    .ToList();return Json(users);

Mockup fiddle #1 here.

You could create a partial data model that deserializes only the properties you need for filtering, with the remaining JSON bound to a [JsonExtensionDataAttribute] property. This should allow you to implement the necessary filtering without needing to hardcode an entire data model.

To do this, define the following model:

public class UserObject{    [JsonPropertyName("id")]    public long Id { get; set; }    [System.Text.Json.Serialization.JsonExtensionDataAttribute]    public IDictionary<string, object> ExtensionData { get; set; }}

And deserialize and filter as follows:

var users = JsonSerializer.Deserialize<List<UserObject>>(rawJsonDownload);users.RemoveAll(u => shouldSkip(u.Id));return Json(users);

This approach ensures that properties relevant to filtering can be deserialized appropriately without needing to make any assumptions about the remainder of the JSON. While this isn't as quite easy as using LINQ to JSON, the total code complexity is bounded by the complexity of the filtering checks, not the complexity of the JSON. And in fact my opinion is that this approach is, in practice, a little easier to work with than the pure JsonDocument approach because it makes it somewhat easier to inject modifications to the JSON if required later.

Mockup fiddle #2 here.

No matter which you choose, you might consider ditching WebClient for HttpClient and using async deserialization. E.g.:

var httpClient = new HttpClient();using var usersDocument = await JsonDocument.ParseAsync(await httpClient.GetStreamAsync("WEB API CALL"));

Or

var users = await JsonSerializer.DeserializeAsync<List<UserObject>>(await httpClient.GetStreamAsync("WEB API CALL"));

You would need to convert your API method to be async as well.