question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use JSONPath expressions like @.length

See original GitHub issue

I am currently working on a project where users can add JSONPath strings to a configuration file to pull specific property values from a JSON object. Currenlty, I am running into a situation where I would like to pull out a property from the last item in an array. For demonstration purposes, I am using this “store” JSON object.

{
	"store" : {
		"book" : [{
				"category" : "reference",
				"author" : "Nigel Rees",
				"title" : "Sayings of the Century",
				"price" : 8.95
			}, {
				"category" : "fiction",
				"author" : "Evelyn Waugh",
				"title" : "Sword of Honour",
				"price" : 12.99
			}, {
				"category" : "fiction",
				"author" : "Herman Melville",
				"title" : "Moby Dick",
				"isbn" : "0-553-21311-3",
				"price" : 8.99
			}, {
				"category" : "fiction",
				"author" : "J. R. R. Tolkien",
				"title" : "The Lord of the Rings",
				"isbn" : "0-395-19395-8",
				"price" : 22.99
			}
		],
		"bicycle" : {
			"color" : "red",
			"price" : 19.95
		}
	}
}

Now I would like to pull the author of the last book on the list using JSONPath and JObject.SelectToken() only. Looking around at available documentation on JSONPath (http://goessner.net/articles/JsonPath/), the solution would be to use an expression like [(@.length-1)] to pull the final result in a JSON array using only a JSONPath string. Below is a code example of what I would like to do assuming the JSON object listed above is contained within the string variable ‘json’.

Sample C# Code / Steps to reproduce.

string jsonPath = "$.store.book[(@.length-1)].author";

JObject jobj = JObject.Parse(json);

string output = jobj.SelectToken(jsonPath).ToString();

Expected behavior

I would expect the output variable to contain a value of “J. R. R. Tolkien”.

Actual behavior

Instead, I receive the following exception.

Unexpected character while parsing path indexer: ( at Newtonsoft.Json.Linq.JsonPath.JPath.ParseArrayIndexer(Char indexerCloseChar) at Newtonsoft.Json.Linq.JsonPath.JPath.ParseIndexer(Char indexerOpenChar, Boolean scan) at Newtonsoft.Json.Linq.JsonPath.JPath.ParsePath(List`1 filters, Int32 currentPartStartIndex, Boolean query) at Newtonsoft.Json.Linq.JsonPath.JPath.ParseMain() at Newtonsoft.Json.Linq.JsonPath.JPath…ctor(String expression) at Newtonsoft.Json.Linq.JToken.SelectToken(String path, Boolean errorWhenNoMatch) at Newtonsoft.Json.Linq.JToken.SelectToken(String path)

Is Newtonsoft.Json capable of handling JSONPath expressions like these? If so, what are the best ways to go about doing this while only using a JSONPath string and the JObject.GetToken() method. If this library is not capable of handling these expressions, then I feel that this is a noted feature gap and should be addressed in future releases.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
NicolasHumanncommented, Jul 10, 2018

Any update of the support of the length function ?

2reactions
Thainacommented, Jan 13, 2019

What will happen if there was a field named length in the path of @ ?

I think the origin jsonpath creator actually leave mistake in document. This kind of things such as length should not be normal expression, it actually should be function

Hence it should be

string jsonPath = "$.store.book[(@.length() - 1)].author";

The implementation of jsonpath in java is also put some expression to be function, such as min along with length https://github.com/json-path/JsonPath#functions

But I have seen you said that it would be too complicate in #209

However I think we should be able to pull it off if we define it as function and checking with () syntax in the parser, even including custom function should be possible, shouldn’t it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array size using jsonpath expression - Stefan Goessner ...
I'm using the version json-path-2.0.0. My jsonpath expression is $.orders.length and JSON looks something like this: { "orders ...
Read more >
Writing JSONPath Expressions - Hevo Data
JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON...
Read more >
JSON path syntax in detail
Assertible's JSON path assertion syntax allows you to get capture values from JSON response bodies using a simple and familiar syntax.
Read more >
JSONPath: Query expressions for JSON
A JSONPath query is a string which selects zero or more nodes of a piece of JSON. · A query MUST be encoded...
Read more >
Introduction to JsonPath
An introduction to Jayway JsonPath, a Java implementation of the JSONPath ... We mostly use it as part of input expressions for predicates....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found