public ActionResult <PathQueryOutput> QueryPath([FromBody] PathQueryInput input) { if (!ModelState.IsValid) { return(BadRequest(new LogicProcessOutput { Errors = ModelState.Root.GetErrors().ToList() })); } var path = JsonPath.Parse(input.Path); var data = input.Data.RootElement; var options = new PathEvaluationOptions { ExperimentalFeatures = { ProcessDataReferences = input.Options?.ResolveReferences ?? false } }; var result = path.Evaluate(data, options); return(Ok(new PathQueryOutput { Result = result })); }
static void Main(string[] args) { string jsonString = ""; string s; while ((s = Console.ReadLine()) != null) { jsonString += s; } var o = JsonValue.Parse(jsonString); var selector = args[0]; try { var path = JsonPath.Parse(selector); var results = path.Evaluate(o); Console.WriteLine(results.ToString()); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } }
public void GoessnerExample9Parsed() { var path = JsonPath.Parse("$..book[?(@.price<10)]"); var expected = new JsonArray { new JsonObject { { "category", "reference" }, { "author", "Nigel Rees" }, { "title", "Sayings of the Century" }, { "price", 8.95 } }, new JsonObject { { "category", "fiction" }, { "author", "Herman Melville" }, { "title", "Moby Dick" }, { "isbn", "0-553-21311-3" }, { "price", 8.99 } } }; var actual = path.Evaluate(GoessnerData); Assert.AreEqual(expected, actual); }
private static void CompareEval(JsonPath expected, string text) { var data = new JsonArray { 1, new JsonObject { ["bool"] = false, ["int"] = 20, ["string"] = "value", }, "hello", true, 5, new JsonArray { 1, 2, 3 } }; var actual = JsonPath.Parse(text); var expectedResult = expected.Evaluate(data); var actualResult = actual.Evaluate(data); Assert.AreEqual(expectedResult, actualResult); }
static void Main(string[] args) { string jsonString = ""; string s; while ((s = Console.ReadLine()) != null) { jsonString += s; } var o = JsonDocument.Parse(jsonString).RootElement; var selector = args[0]; JsonPath path = null; try { path = JsonPath.Parse(selector); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(2); } try { var results = path.Evaluate(o); Console.WriteLine(results.Matches.Select(m => m.Value).AsJsonElement().ToJsonString()); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(1); } }
public void GoessnerExample8Parsed() { var path = JsonPath.Parse("$..book[?(@.isbn)]"); var expected = new JsonArray { new JsonObject { { "category", "fiction" }, { "author", "Herman Melville" }, { "title", "Moby Dick" }, { "isbn", "0-553-21311-3" }, { "price", 8.99 } }, new JsonObject { { "category", "fiction" }, { "author", "J. R. R. Tolkien" }, { "title", "The Lord of the Rings" }, { "isbn", "0-395-19395-8" }, { "price", 22.99 } } }; var actual = path.Evaluate(GoessnerData); Assert.AreEqual(expected, actual); }
public void Issue16_PathIntolerantOfUnderscoresInPropertyNames() { var expected = JsonPathWith.Name("properties").Name("id_person"); var actual = JsonPath.Parse("$.properties.id_person"); Assert.AreEqual(expected.ToString(), actual.ToString()); }
public void SlicedIndexedArray() { var text = "$[1:5,7]"; var expected = JsonPathWith.Array(new Slice(1, 5), 7); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void SteppedSlicedArray() { var text = "$[1:5:2]"; var expected = JsonPathWith.Array(new Slice(1, 5, 2)); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void NamedObjectWithWildcardSearch() { var text = "$.name..*"; var expected = JsonPathWith.Name("name").Search(); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void MultiSlicedArray() { var text = "$[1:5,7:11:2]"; var expected = JsonPathWith.Array(new Slice(1, 5), new Slice(7, 11, 2)); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void MultiIndexedArray() { var text = "$[1,3]"; var expected = JsonPathWith.Array(1, 3); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
private JsonArray Evaluate(string jsonString, string pathString) { var o = JsonValue.Parse(jsonString); var selector = pathString; var path = JsonPath.Parse(selector); var results = path.Evaluate(o); return(results); }
public void WildcardObjectWithNamedObject() { var text = "$.*.name"; var expected = JsonPathWith.Name().Name("name"); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void DoubleQuotedNamedSearch() { var text = "$..\"quoted name\""; var expected = JsonPathWith.Search("quoted name"); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void SingleWildcardSearch() { var text = "$..*"; var expected = JsonPathWith.Search(); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void SingleNamedSearch() { var text = "$..name"; var expected = JsonPathWith.Search("name"); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void SearchIndexedArray() { var text = "$..[1]"; var expected = JsonPathWith.SearchArray(1); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void WildcardArray() { var text = "$[*]"; var expected = JsonPathWith.Array(); var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); }
public void GoessnerExample4Parsed() { var path = JsonPath.Parse("$.store..price"); var expected = new JsonArray { 8.95, 12.99, 8.99, 22.99, 19.95 }; var actual = path.Evaluate(GoessnerData); Assert.AreEqual(expected, actual); }
public void GoessnerExample2Parsed() { var path = JsonPath.Parse("$..author"); var expected = new JsonArray { "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien" }; var actual = path.Evaluate(GoessnerData); Assert.AreEqual(expected, actual); }
public void Issue31_JsonPathArrayOperatorShouldWorkOnObjects_Parsed() { var json = GoessnerExamplesTest.GoessnerData; var path = JsonPath.Parse(@"$.store.bicycle[?(1==1)]"); var results = path.Evaluate(json); Assert.AreEqual(new JsonArray { "red", 19.95 }, results); }
public void Example10() { var input = "$..*"; var path = JsonPath.Parse(input); var result = path.Evaluate(_instance); Assert.IsNull(result.Error); Assert.AreEqual(27, result.Matches.Count); Assert.AreEqual(input, path.ToString()); }
public void Example6b() { var input = "$..book[-1:]"; var path = JsonPath.Parse(input); var result = path.Evaluate(_instance); Assert.IsNull(result.Error); Assert.AreEqual(1, result.Matches.Count); Assert.AreEqual("The Lord of the Rings", result.Matches[0].Value.GetProperty("title").GetString()); Assert.AreEqual(input, path.ToString()); }
public void GrammarExample() { var input = "$.store.book[0].title"; var path = JsonPath.Parse(input); var result = path.Evaluate(_instance); Assert.IsNull(result.Error); Assert.AreEqual(1, result.Matches.Count); Assert.AreEqual("Sayings of the Century", result.Matches[0].Value.GetString()); Assert.AreEqual(input, path.ToString()); }
public string GetStringValue(JsonValue jsonValue, string jsonPath) { if (string.IsNullOrEmpty(jsonPath)) { return(string.Empty); } var path = JsonPath.Parse(jsonPath); var result = path.Evaluate(jsonValue).FirstOrDefault(); return(result?.String ?? string.Empty); }
public void Issue243_OverlappingBoundsReturnsValuesInOverlap() { var json = new JsonArray { "first", "second", "third" }; var path = JsonPath.Parse("$[1:10]"); var results = path.Evaluate(json); Assert.AreEqual(new JsonArray { "second", "third" }, results); }
public void Example9() { var input = "$..book[?(@.price<10)]"; var path = JsonPath.Parse(input); var result = path.Evaluate(_instance); Assert.IsNull(result.Error); Assert.AreEqual(2, result.Matches.Count); Assert.AreEqual("Sayings of the Century", result.Matches[0].Value.GetProperty("title").GetString()); Assert.AreEqual("Moby Dick", result.Matches[1].Value.GetProperty("title").GetString()); Assert.AreEqual(input, path.ToString()); }
public void Example3() { var input = "$.store.*"; var path = JsonPath.Parse(input); var result = path.Evaluate(_instance); Assert.IsNull(result.Error); Assert.AreEqual(2, result.Matches.Count); Assert.AreEqual(4, result.Matches[0].Value.EnumerateArray().Count()); Assert.AreEqual(2, result.Matches[1].Value.EnumerateObject().Count()); Assert.AreEqual(input, path.ToString()); }
public void FromJson(JsonValue json, JsonSerializer serializer) { Path = JsonPath.Parse(json.Object["path"].String); var result = json.Object["result"]; if (result.Type == JsonValueType.Boolean && !result.Boolean) { Result = new JsonArray(); } else { Result = result.Array; } }