-2

I'm getting the response below. I need to assert the value of "Type". How can I get the value? I'm using Java and rest-assured

The response:

"Product1": {
    "Productive1": {
        "applause": {
            "home-Only": {
                "Component-write": {
                    "Default": [
                        {
                            "Type": "123456",
                            "Name": "product-1",
                            "profile": "employee",

                        }
                    ]
                }
            },
            "office -Only": {
                "Fee": {
                    "Default": [
                        {
                            "Type": "123456",
                            "Name": "Produc-2",
                            "profile": "boss",

                        }
                    ]
                }
            }
        }
    }
}

I'm trying with this code:

List<String> ObjectList1 = new ArrayList<String>();

            JSONObject jsonObject= new JSONObject(Generic.response.getBody().asString());

            Set<String> a = jsonObject.keySet();

            for (Object objj : a) {
                    String element = (String) objj;
                    ObjectList1.add(element);
            }

            for (int i = 0; i < ObjectList1.size()-1; i++) {
                    JSONArray obj1 = jsonObject.getJSONObject(ObjectList1.get(i)).names();
                    System.out.println(obj1);
            }
4
  • 1
    What have you tried so far? Apr 21 '20 at 12:32
  • Provide more details to the problem that you are facing just copy pasting the code doesn't help. Also go through the link How to ask a good question and [Help Center].(stackoverflow.com/help)
    – Alok
    Apr 22 '20 at 5:52
  • What results do you get from running your code?
    – Kate Paulk
    Apr 22 '20 at 11:19
  • JSONObject["Product1"] not found i'm getting this error Apr 22 '20 at 12:31
1

What you are showing is called a JSON object. Unless you're in JavaScript (that can natively parse JSON data) you can use a jsonpath language that is very similar to xpath (serves for similar purpose). Using jsonpath query you can access any JSON part (including what you're looking for). Here are few refs for different tools/languages (not neccessery related to JSONpath):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.