-1

I am pretty new to java and rest. I was trying to count elements from the item object using rest. If you check below Json then you will find 6 elements associated with the item object.

{
  "response": {
    "status": "active",

    "timestamp": "2019-01-02 20:07:42"
  },

  "group": [
    {

      "best_option": "Offer",
      "item": {
        "F": [
          {
            "code": "228"
          }
        ],
        "E": [
          {
            "code": "228"
          }
        ]
      },
      "review": {
        "F": [
          {
            "code": "110"
          },
          {
            "code": "212"
          }
        ],
        "E": [
          {
            "code": "110"
          },
          {
            "code": "212"
          }
        ]
      }
    },
    {
      "best_option": "Offer",
      "item": {
        "D": [
          {
            "code": "228"
          }
        ],
        "C": [
          {
            "code": "228"
          }
        ]
      },
      "review": {
        "D": [
          {
            "code": "110"
          },
          {
            "code": "212"
          }
        ],
        "C": [
          {
            "code": "110"
          },
          {
            "code": "212"
          },
          {
            "code": ""
          }
        ]
      }
    },
    {
      "best_option": "Offer",
      "item": {
        "A": [
          {
            "code": "228"
          }
        ]
      },
      "review": {
        "A": [
          {
            "code": "110"
          },
          {
            "code": "212"
          }
        ]
      }
    },
    {

      "best_option": "Offer",
      "item": {
        "B": [
          {
            "code": "228"
          },
          {
            "code": "662"
          }
        ]
      },
      "review": {
        "B": [
          {
            "code": "110"

          },
          {
            "code": "662"
          }
        ]
      }
    }
  ]
}

I have tried jsonResponse.getBody().jsonPath().get() to count element from item object. I want to count number of elements present in the item object for e.g: A, B, C,D,E F . Can someone please help me to sort out my issue?

7
  • 2
    Possible duplicate of JSON array get length Commented Jan 2, 2019 at 9:25
  • 1
    I don't think so this is duplicate. I already check above solution and i am not trying to check json array . please read my question clearly Commented Jan 2, 2019 at 9:27
  • "still facing issue" is not a clear statement of your problem. Please explain what issue you are facing exactly. Commented Jan 2, 2019 at 9:29
  • I am not able to count element from item object. i want to count number of elements present in the item object for e.g: A, B, C,D,E F Commented Jan 2, 2019 at 9:36
  • 1
    Iterate the JSON items and save the item.*.code in a Set so that you can get the distinct value easily. This is quite simple to do but base on the JSON library you are using (if you are using one...) the solution may vary so I can't help you more Commented Jan 2, 2019 at 10:00

1 Answer 1

4

Iterate over "group" object, get "item" as JsonObject and get the size.

    JsonObject jsonObject = (JsonObject)new JsonParser().parse(input);
    JsonArray groupObject = jsonObject.getAsJsonArray("group");

    int countItem=0;
    for(int i=0;i<groupObject.size();i++) {
        JsonObject items = ((JsonObject)groupObject.get(i)).getAsJsonObject("item");
        countItem+=items.size();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What library are you using?

Your Answer

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