1

I have searched a lot but no answer helped what I want.

[{
    "activity_id": 1,
    "created_date": 104,
    "note_id": 1
}, {
    "activity_id": 2,
    "created_date": 101,
    "note_id": 2
}, {
    "activity_id": 3,
    "created_date": 105,
    "note_id": 3
}, {
    "activity_id": 4,
    "created_date": 102,
    "note_id": 4
}, {
    "activity_id": 5,
    "created_date": 103,
    "note_id": 5
}]

DocumentContext dc =  JsonPath.parse(data);

I have tried to get key d.read(key) but it didn't work as it gives an array of values.

How can I get an array of all values of a particular key? There is one thing to add more. I can get array of all values using d.read("$..created_date") But I have to make generic functions so I will take the key name as a parameter and I have to find all values of that given key.

For example: created_date: [101, 102, 103..]

I tried using Jackson also. I cannot use a library other than this.

Any help will be appreciated.

1 Answer 1

2

Hope this code works for your requirement

Code:

import java.util.List;

import com.jayway.jsonpath.JsonPath;

public class TestJsonPath {

    public static void main(String[] args) {
        String key = "created_date";
        testJsonPath(key);
    }

    private static void testJsonPath(String key) {

        String json = "{\"list\" : { \"time\": [{\"activity_id\":1,\"created_date\":104,\"note_id\":1},{\"activity_id\":2,\"created_date\":101,\"note_id\":2},{\"activity_id\":3,\"created_date\":105,\"note_id\":3},{\"activity_id\":4,\"created_date\":102,\"note_id\":4},{\"activity_id\":5,\"created_date\":103,\"note_id\":5}] } }";
        System.out.println(json);
        List<Object> createdDate = JsonPath.read(json, String.format("$.list.time[*].%s", key));
        System.out.println(createdDate);

    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. I found the values using $..created_date. I have updated this in question as well. But I will get key value from parameters so I have no idea What will be there it could be note_id, activity_id or created_date
@MaheshChand U get compare key & value using this stackoverflow.com/questions/43798853/… and once u get the proper key value match , write on method and pass that key to "JsonPath.read(json, "$.list.time[*].<<key>>");"
Thanks again but this I am getting JSON from third party API. I can't modify it. So can you help me without using time or list. Even I am able to find d.key("$..created_date") .
Hey, thanks you have helped me a lot. I found a way to do this. Modify your answer I will give you points. d.read(String.format("$..%s", sortingKey)) sortingKey is that key which holds the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.