using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Text.Json.Nodes;
using Newtonsoft.Json.Linq;
public static void Test()
static void Test(string myKey)
var newtonsoftString = TestNewtonsoft(GetJson(), myKey);
Console.WriteLine("Newtonsoft result for key {0}: {1}", myKey, newtonsoftString);
var systemString = QueryJson(GetJson(), myKey, true);
Console.WriteLine("System.Text.Json.Nodes result for key {0}: {1}", myKey, systemString);
var docString = QueryJsonWithJsonDocument(GetJson(), myKey, true);
Console.WriteLine("JsonDocument result for key {0}: {1}", myKey, docString);
Assert.AreEqual(newtonsoftString, QueryJson(GetJson(), myKey, true));
Assert.AreEqual(newtonsoftString, QueryJsonWithJsonDocument(GetJson(), myKey, true));
static string? QueryJson(string json, string myKey, bool addComma = false)
var node = JsonNode.Parse(json)!.AsObject();
var query = (node[myKey] as JsonArray)?.Select(i => i?.ToString().ToUpper());
query = query.Concat(new [] { "" });
return string.Join(',', query);
static string? QueryJsonWithJsonDocument(string json, string myKey, bool addComma = false)
using var doc = JsonDocument.Parse(json);
var query = doc.RootElement.Get(myKey)?.EnumerateArray().Select(e => e.GetString()?.ToUpper());
query = query.Concat(new [] { "" });
return string.Join(',', query);
static string TestNewtonsoft(string json, string myKey)
object jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(object));
foreach (var jsonList in ((Newtonsoft.Json.Linq.JObject)(jsonObject)))
if (jsonList.Key == myKey)
foreach (object o in (jsonList.Value.ToArray()))
myList += o.ToString().ToUpper() + ',';
static string GetJson() => @"{""myKey"":[""fir"",""dsdsd""],""status"":""ok""}";
public static partial class JsonExtensions
public static JsonElement? Get(this JsonElement element, string name) =>
element.ValueKind != JsonValueKind.Null && element.ValueKind != JsonValueKind.Undefined && element.TryGetProperty(name, out var value)
? value : (JsonElement?)null;
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");