using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static void Test()
TestThatSelectTokenThrowsOnMultipleMatches();
public static void Test1()
var o = JToken.Parse(GetJson());
var query = "$..Products[?(@.Name == 'AAA' || @.Name == 'BBB' || @.Name == 'CCC')].Price";
var prices = o.SelectTokens(query);
Console.WriteLine(JsonConvert.SerializeObject(prices, Formatting.Indented));
Assert.AreEqual("[10.01,30.03]", JsonConvert.SerializeObject(prices, Formatting.None));
public static void Test2()
var o = JToken.Parse(GetJson());
var names = new []{ "AAA", "BBB", "CCC" };
var query = names.Select((n, i) => (n, i))
.Aggregate(new StringBuilder("$..Products[?("),
(sb, p) => sb.Append(p.i == 0 ? "" : "||").Append("@.Name=='").Append(p.n).Append("'"))
var prices = o.SelectTokens(query);
Console.WriteLine(query);
Console.WriteLine(JsonConvert.SerializeObject(prices, Formatting.Indented));
Assert.AreEqual("[10.01,30.03]", JsonConvert.SerializeObject(prices, Formatting.None));
static void TestThatSelectTokenThrowsOnMultipleMatches()
var o = JToken.Parse(GetJson());
var query = "$..Products[?(@.Name == 'AAA' || @.Name == 'BBB' || @.Name == 'CCC')].Price";
Assert.Throws<JsonException>(() => o.SelectToken(query));
static string GetJson() => @"{
{""Name"" : ""AAA"", ""Price"":10.01},
{""Name"" : ""DDD"", ""Price"":20.02},
{""Name"" : ""CCC"", ""Price"":30.03}
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");