Runtime: Add JsonPath support to JsonDocument/JsonElement

Created on 4 Oct 2019  路  14Comments  路  Source: dotnet/runtime

Hi,

I'd like to request JsonPath support for querying the JsonDocument/JsonElement classes. JsonPath provides similar capability to XPath (and even Sql) in that it allows queries to be performed against Json documents. This currently represents a major gap for us shifting from Newtonsoft to system.text.json, as we provide JsonPath values as an input parameter to a data load process which can't be worked around via code.

The JsonPath syntax is described here.

The equivalent functionality in the Newtonsoft library is:

var jsonPath = "$.my.path";
var json = JToken.Parse(jsonString);
var token = json.SelectToken(jsonPath);

The following proposed syntax would work well in the JsonDocument structure:

var jsonPath = "$.my.path";
var jsonDoc = JsonDocument.Parse(json);
var element = jsonDoc.SelectElement(jsonPath); //returns JsonElement
var elements = jsonDoc.SelectElements(jsonPath); //returns JsonElement.ArrayEnumerator

Thanks.

api-suggestion area-System.Text.Json

Most helpful comment

Would also like to push this up the stack. We want to use system.text.json but the lack of JsonPath is blocking us.

All 14 comments

Just searched for a similar thing myself. The appropriate newtonsoft documentation for this: https://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm

Seconding this. It would be extremely useful.

Would also like to push this up the stack. We want to use system.text.json but the lack of JsonPath is blocking us.

In the meantime. I'm doing some experiment following the same strategy of the Json.net for support JsonPath but using System.Text.Json.JsonDocument https://github.com/azambrano/JsonDocumentPath

@layomia did this and writedom move out of scope for 5.0?

If this is implemented it would be nice to have the option for case insensitivity.

We want this

This would be very useful

I've ported the Newtonsoft.Json implementation to work with JsonDocument, along with the tests.

nuget: https://www.nuget.org/packages/BlushingPenguin.JsonPath/
source: https://github.com/blushingpenguin/BlushingPenguin.JsonPath/

I've ported the Newtonsoft.Json implementation to work with JsonDocument, along with the tests.

nuget: https://www.nuget.org/packages/BlushingPenguin.JsonPath/
source: https://github.com/blushingpenguin/BlushingPenguin.JsonPath/

Hello, what's the difference with this? https://github.com/azambrano/JsonDocumentPath

It's pretty similar (it's also a port of Newtonsoft.Json's implementation), but is missing some of the functionality of the original and lacks a nuget package. (AFAIK I've ported all of the original functionality).

I could have forked that one, but my version is actually a port of https://github.com/blushingpenguin/MongoDB.Bson.Path (which is a version that works on the MongoDB.BsonDocument family).

More support for JSON Path

Does it merged into .NET 5?

@ADustyOldMuffin, what's with the downvote without an explanation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matty-hall picture matty-hall  路  3Comments

yahorsi picture yahorsi  路  3Comments

Timovzl picture Timovzl  路  3Comments

aggieben picture aggieben  路  3Comments

chunseoklee picture chunseoklee  路  3Comments