using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static partial class JTokenExtensions
public static JObject [] FilterObjects<T>(this JObject root, string someProp, T someValue)
var comparer = new JTokenEqualityComparer();
var someValueToken = JToken.FromObject(someValue);
var objs = root.DescendantsAndSelf()
.Where(t => comparer.Equals(t[someProp], someValueToken))
public static partial class JTokenExtensions
public static bool IsNull(this JToken token)
return token == null || token.Type == JTokenType.Null;
public static JObject[] FilterObjectsSimple<T>(this JObject root, string someProp, T someValue)
var comparer = EqualityComparer<T>.Default;
var objs = root.DescendantsAndSelf()
.Where(t => { var v = t[someProp]; return v != null && (someValue == null ? v.IsNull() : comparer.Equals(v.ToObject<T>(), someValue)); })
public static void Test()
var jsonString = GetJson();
var root = JObject.Parse(jsonString);
var allObjs = root.DescendantsAndSelf()
Console.WriteLine("\nCount of all objects: {0}", allObjs.Length);
Assert.IsTrue(allObjs.Length == 7);
TestFilterObjects(jsonString, "id", "3", 2);
TestFilterObjectsSimple(jsonString, "id", "3", 2);
TestSelectTokens(jsonString);
catch (AssertionFailedException ex)
Console.WriteLine("Searching using JSONPath failed: " + ex.Message);
Console.WriteLine("\nDone");
static void TestFilterObjects<T>(string jsonString, string someProp, T someValue, int expectedCount)
var root = JObject.Parse(jsonString);
var filteredObjs = root.FilterObjects(someProp, someValue);
Console.WriteLine("\nAll objects with {0} == {1}:", someProp, someValue);
Console.WriteLine(JsonConvert.SerializeObject(filteredObjs, Formatting.Indented));
Assert.IsTrue(filteredObjs.Length == expectedCount);
static void TestFilterObjectsSimple<T>(string jsonString, string someProp, T someValue, int expectedCount)
var root = JObject.Parse(jsonString);
var filteredObjs = root.FilterObjectsSimple(someProp, someValue);
Console.WriteLine("\nAll objects with {0} == {1}:", someProp, someValue);
Console.WriteLine(JsonConvert.SerializeObject(filteredObjs, Formatting.Indented));
Assert.IsTrue(filteredObjs.Length == expectedCount);
static void TestSelectTokens(string jsonString)
var root = JObject.Parse(jsonString);
var filterString = string.Format(@"..*[?(@.{0} == '{1}')]", someProp, someValue);
var filteredObjs = root.SelectTokens(filterString).ToArray();
Console.WriteLine("\nTesting query string \"{0}\", all objects with {1} == {2}:", filterString, someProp, someValue);
Console.WriteLine(JsonConvert.SerializeObject(filteredObjs, Formatting.Indented));
Assert.IsTrue(filteredObjs.Length == 2, "filteredObjs.Length != 2");
""name"" : ""carparkone"",
""model"" : ""M7 With No Id"",
""model"" : ""M7 With Null Id"",
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");