-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);
            }
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):

| improve this answer | |

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.