0

Is it possible to get a new JsonPath object out of a current one? The only thing I've found is setRootPath() but that's not exactly what I'm looking for since you cannot "go back" once you've set the root.

My use case:
I have a JSON array of objects. I'd like to go over the JSON array and pass each object to some function. The thing is, I don't want to deserialize the object nor to pass a LinkedHashMap. Instead, I'd like to pass a JsonPath that represents the object alone.

Additional Info:

Let's assume we have the following JSON:

[
{"name": "David"},{"name": "Dana"},{"name": "Ron"}
]

And we've just read it using JsonPath:

JsonPath = JsonPath.from(json);

I'd like to iterate over the objects and for each object have a JsonPath that represents it. The only way to do it (as far as I can tell) is to use setRootPath() but once you do that you can't go back.

IsaacLevon
  • 2,260
  • 4
  • 41
  • 83
  • The description is a bit vague, can you give additional details like an example or a code that you have tried out already – Wilfred Clement Sep 15 '20 at 11:37
  • still can't see any code @yaseco. It'd be easier too see a valid json, a code that you have tried already, place where you are blocked etc. Honestly I would not want to break my head trying to understand the use case. I'd suggest you edit the question even further or maybe just leave it be and somebody else can take a look at it maybe :) – Wilfred Clement Sep 15 '20 at 12:18

1 Answers1

0
//A bit confusing about what was asked here. But try this, hope it will help 
//If you have a json object already no need to initiate the jsonObject
JSONObject jsonObject = new JSONObject();
String jsonString = jsonObject.toString();

String path = "$.rootObject.childObject"

//Only returning the child object
JSONObject j = JsonPath.read(jsonString, path);

//Returning the array of string type from the child object. E.g
//{"root": "child":[x, y, z]}

List<String> values = sonPath.read(jsonString, path);
Md Nuhel
  • 75
  • 1
  • 6