Search code examples
javaarraysjsonrest-assuredrest-assured-jsonpath

How to send array value for one the key for rest assured request


Below is the expected request which should hit the system but while sending it the array becoming string?

Expected { "class": "A", "subUsecases": [ "string1", "String2" ] }

To generate the above, I am creating the Array object and sending the request but its turning out to be some like this in rest assured request.

Actual

{"Class": "A", "subClass": "[\"String1\",\"String2\"]"}

Due to above actuall result, api is thinking it a string for subClass and not treating it as array. But code wants it to be an array for subClass.

I am using hasmap to create the above. Like this

@Test
    public void mm(){
        HashMap<String,String> queryParam = new HashMap<>();
        queryParam.put("CLASS","A");
        queryParam.put("subClass", arrayMethod("String1","String2"));
    }

    public String arrayMethod(String s1, String s2){
        org.json.simple.JSONArray array = new JSONArray();

        array.add(s1);
        array.add(s2);

        return array.toJSONString();
    }

queryParam is going as jsonbody.

Now How to send as expected json body instead of actual json body. Thanks in advance.


Solution

  • Change Map to:

    HashMap<String, Object> queryParam = new HashMap<>();
    

    And create a regular list or array instead of using JSONArray object. array.toJSONString() produces already JSON string which you put as value to another object.

    queryParam.put("subClass", Arrays.asList("String1", "String2"));