What you are trying to do is also called as Xpath in technical terms. It is used for html, xml based languages as well as now available in json
You can try jsonpath for this case:
https://www.baeldung.com/guide-to-jayway-jsonpath https://github.com/json-path/JsonPath
Answer from swapyonubuntu on Stack OverflowWhat you are trying to do is also called as Xpath in technical terms. It is used for html, xml based languages as well as now available in json
You can try jsonpath for this case:
https://www.baeldung.com/guide-to-jayway-jsonpath https://github.com/json-path/JsonPath
Google's popular GSON library has a method, namely getPath, maybe useful for your purpose:
String json = "...";
JsonReader reader = new JsonReader(new StringReader(json));
System.out.println(reader.getPath());
With Jackson's tree model (JsonNode), you have both "literal" accessor methods (get), which returns null for missing value, and "safe" accessors (path), which allow you to traverse "missing" nodes. So, for example:
JsonNode root = mapper.readTree(inputSource);
int h = root.path("response").path("history").getValueAsInt();
which would return the value at the given path, or, if the path is missing, 0 (default value).
But more conveniently, you can just use JSON pointer expression:
int h = root.at("/response/history").getValueAsInt();
There are other ways too, and often it is more convenient to model your structure as a Plain Old Java Object (POJO). Your content could fit something like:
public class Wrapper {
public Response response;
}
public class Response {
public Map<String,Integer> features; // or maybe Map<String,Object>
public List<HistoryItem> history;
}
public class HistoryItem {
public MyDate date; // or just Map<String,String>
// ... and so forth
}
and if so, you would traverse the resulting objects just like any Java object.
Use Jsonpath
Integer h = JsonPath.parse(json).read("$.response.repository.history", Integer.class);
I'm not familiar with Ready API or the json-path and json-smart libraries, but with pure groovy you can just do:
import groovy.json.*
def str = """<the json string in your question>"""
def json = new JsonSlurper().parseText(str)
def memberNames = json.members*.name
println memberNames.join(", ")
which when executed will print:
Molecule Man, Madame Uppercut, Eternal Flame
JsonSlurper returns a java.util.Map of maps structure which can be navigated using normal groovy findAll, collect, etc operations or the spead operator (*.) as in the above.
Placing asm-1.0.2.jar file in Ready API lib folder resolved the issue.
Using the path is currently (.NET 7) not supported. It might be supported in a later version.
From documentation:
The JsonDocument DOM doesn't support querying by using JSON Path.
In a JsonNode DOM, each JsonNode instance has a GetPath method that returns a path to that node. But there is no built-in API to handle queries based on JSON Path query strings.
If you need very flexible Json support, use the Newtonsoft.Json Nuget package.
Although this is an old post, this came top in Google search. This is how I implemented using path;
extension method to JsonNode
private static JsonNode GetNodeUsingPath(this JsonNode root, string path)
{
if (root == null || string.IsNullOrEmpty(path))
return null;
string[] parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
JsonNode currentNode = root;
// ignore 0th element as it is for root $
for (int x = 1; x < parts.Length; x++)
{
if (currentNode == null)
return null;
currentNode = currentNode[parts[x]];
}
return currentNode;
}
usage;
string path = "$.cakes.cheese";
JsonNode cheeseNode = rootNode.GetNodeUsingPath(path);