0

I have written code for Rest Assured API to automate, I've created the payload using JSONObject but the payload is not ordered because of which I am getting an error. Below is how my payload should look like

{
    "Student": "Primary",
    "class": "First",
    "segments": [{
            "Subject": "Science",
            "Marks": "50"

        },
        {

            "Subject": "History",
            "Marks": "50"
        }
    ],
    "Fee": "Paid",
    "Roll no": "20"
}

but the payload generated by the object mapper is as below

 {
    "Student": "Primary",
    "class": "First",
    "Fee": "Paid",
    "Roll no": "20",

    "segments": [{
            "Subject": "Science",
            "Marks": "50"

        },
        {

            "Subject": "History",
            "Marks": "50"
        }
    ]
}

Below is the code written in Java

JSONObject requestParam = new JSONObject();
JSONArray requestParamArray = new JSONArray();

requestParam.put("Student", strStud);
requestParam.put("class", strClass);

Map searchReqMap1 = new LinkedHashMap(3);
searchReqMap1.put("Subject", strsubject);
searchReqMap1.put("mark", strmark);

Map searchReqMap2 = new LinkedHashMap(3);
searchReqMap2.put("Subject", strsubject);
searchReqMap2.put("mark", strmark);

requestParamArray.add(searchReqMap1);
requestParamArray.add(searchReqMap2);

requestParam.put("segments", requestParamArray);
requestParam.put("fee", strfee);
requestParam.put("rollno", strrollno);
return requestParam.toJSONString();

` Input data is read from excel

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
Vipin Singh
  • 109
  • 1
  • 14

1 Answers1

0

Could you give some error log to let us know why you think "but the payload is not ordered because of which I am getting an error."

As far as I know, order of JSON object members does not matter. See this: Does the sequence of the values matter in a JSON object?

Your code looks very good. Only this sentence new LinkedHashMap(3), I think maybe it should be new LinkedHashMap(2).

So please show more info, I will edit this answer later.

Peter Quan
  • 788
  • 4
  • 9