Searching JSON files

JSON

There are huge JSON structures just like XML documents. To search effectively an XML we use XPath, while the equivalent for JSON is the JSONPath

The details of the syntax are explaind here:https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html

Here is an example with Newtonsoft JSON library

JObject rootObject = JObject.Parse("....");
JToken token = rootObject.SelectToken("$..element.child[?(@prop=='data')]");
string data = token.Value<string>();

Here is a website where you can test your JSON path: https://jsonpath.com/

For System.Text.Json there is no support for such queries, however, there is a library that adds this support: https://github.com/stanac/JsonPathway

Post a Comment

Previous Post Next Post