1

I'm having some trouble trying query a JSON with Java JsonPath.

I have a large json with informations about people location (thousand of lines). The field to find the person location is your macAdreess. The macAdreess does not repeat, in other words when I query for a specific macAdreess, I got once result.

This is a small piece of my Json:

{
   "readings": [
      {
         "value": {
            "floorRefId": "-4564288095083560912",
            "x": 86.405304,
            "y": 64.4601,
            "z": 0
         },
         "tags": {
            "macAddress": "f8:e0:79:82:95:92"
         },
         "timestamp": 1494620148598
      },
      {
         "value": {
            "floorRefId": "-4564288095083560912",
            "x": 86.540474,
            "y": 64.12458,
            "z": 0
         },
         "tags": {
            "macAddress": "f4:f5:24:96:d5:cd"
         },
         "timestamp": 1494620148598
      },
      {
         "value": {
            "floorRefId": "-4564288095083560912",
            "x": 86.31584,
            "y": 64.410446,
            "z": 0
         },
         "tags": {
            "macAddress": "f4:f5:24:2a:9c:13"
         },
         "timestamp": 1494620148598
      },
   ],
   "gateway_uuid": "cccccccc-9f70-4d93-94be-2fa7e15ef292",
   "status": "running"
}

I will need to query for one macAddress into JSON every five seconds, so if I need to traverse all JSON content, I will have a performance trouble.

Then I'm trying to use Java JsonPath API to query the macAdreess and it's running ok. But I need to get the parent node to get the location fields.

With this code:

public static void main(String[] args) throws Exception {
    String json = getJsonAsString();
    List<Map<String, Object>> expensive = JsonPath.parse(json).read("$..[?(@.macAddress=='f4:f5:24:96:d5:cd')]");
    System.out.println(expensive);
}

I got thit result:

[{"macAddress":"f4:f5:24:96:d5:cd"}]

2
  • 1
    You say that you will have performance trouble if you traverse the JSON your self. What do you think JsonPath is doing if not traversing the JSON? Commented May 13, 2017 at 22:20
  • It works! About performance trouble you are right! Maybe traversing the JSON by myself I will get more performance! Thanks a lot! Commented May 13, 2017 at 22:51

1 Answer 1

1

You need to query from the parent, and don't use the $.. deep scan.

$.readings[?(@.tags.macAddress=="f4:f5:24:96:d5:cd")]

But if you know there's only one, you'd be better off to linearly scan the data yourself. It would be more performant

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

Comments

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.