0

JSON example:

{
"locale": "abc",
"passengers": [
{
  "address1": "xx",
  "address2": "yy",
  "city": "London",
  "county": "Test",
  "dateOfBirthDay": "2",
  "dateOfBirthMonth": "1",
  "dateOfBirthYear": "1234",
  "email": "t@t.com",
  "firstName": "first",
  "lastName": "test",
  "postcode": "xyz",
  "telephone": "123131321",
  "title": "MR"
},
{
  "dateOfBirthDay": "2",
  "dateOfBirthMonth": "4",
  "dateOfBirthYear": "1234",
  "firstName": "date",
  "lastName": "change",
  "title": "MR"
   }
   ],
  "paymentCard": {
"expiryMonth": "5",
"expiryYear": "2222",
"cardNumber": "12345678901234",
"securityCode": "123",
"cardType": "DEL"
},
"token": "{{tokenvalue}}"
}

Code example:

@Getter
@Builder
@ToString
@AllArgsConstructor
private static class sndRequest {
    private String adults;
    private String children;


}

public String sndRequestMethod(int adults, int children) {
    RestAssured.defaultParser = Parser.JSON;

    qRequest q1Request = qRequest.builder()
            .adults(adults)
            .children(children)
            .build() ;

    String body = gson.toJson(q1Request);

}

This is how I am passing values into the request body, can anyone please help how to pass double list of strings(which I mentioned above) to body with the help of Gson.

How do I send multiple Strings into Passengers? I need something like passengers [ { string1 }, { string 2 } ] - how do I acheive this format?

  • 1
    Please add more explanation to your question. You can read the information at sqa.stackexchange.com/help/how-to-ask for more guidelines on how to ask a good question. – Kate Paulk Feb 2 '18 at 15:58
  • Hope it is clear now ?? can you please help me to understand how to pass the body in json format using gson – LaxmiMaddali Feb 2 '18 at 16:13
  • how do i send multiple Strings into Passengers ?? passengers [ { string1 }, { string 2 } ], - how do i acheive this format ?? – LaxmiMaddali Feb 2 '18 at 16:44
  • Upvoted as this is actually a very good question. Building objects and generating json from them is actually much, much better than passing json strings. One of the major advantages is that when using domain objects, you can actually check that you're building the right thing with fields needed etc, while checking if a json String is valid is nowhere near as easy. – Marit Feb 4 '18 at 6:10
1

You will need to specify the object/objects you are sending with all the of the required fields. You can then use libraries like Gson or Jackson to build the json from those objects.

Implement classes and mark them with Jackson annotations to specify how they should be serialized to json.

For example, something like this (not 100% how to mark your nested structure to match exacly):

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NONE) // Means: wrap this object, don't use a name

@JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class XXX { // Not sure what this is called in your domain

    String locale;
    List<Passenger> passengers;
    PaymentCard card;
    String token
}

// TODO: annotate
class Passenger {
    String address;
    String address2;
    // etc for all other fields
}

// TODO: annotate
class YYY { // // Not sure what this is called in your domain
    // Also, these seem fields of Passenger
    // Should it be in it's own block {}?
     String dateOfBirthDay;
     String dateOfBirthMonth;
    // etc for all other fields
}

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME) // Wrap it and use it's name
class PaymentCard {
    String expiryMonth;
    String expiryYear;
    // etc for all other fields
}

For more information, have a look at: jackson tutorial

  • 2
    Please edit your answer to explain how the OP can specify the object/s and build the JSON/GSON. – Kate Paulk Feb 5 '18 at 12:33
  • The edits I think are sufficient to turn this into a much better answer. – corsiKa Feb 6 '18 at 15:30
0
public void postBookExternalConnects(String locale) {

    RestAssured.defaultParser = Parser.JSON;

    classA obj1 = new classA();
    classA obj2 = new classA();
    //passenger2.setAddress1(null);
    classB obj3 = new classB();

    Request request = Request.builder()
            .xxx(xxx)
            .yyy(Arrays.asList(passenger, passenger2))
            .locale(locale)
            .paymentCard(payment)
            .build();

    String body = gson.toJson(flightOnlyRequest);

    Response response = given()
            .headers("xx, yy")
            .body(body)
            .when().post("url").then()
            .contentType(ContentType.JSON).extract().response();
}


    @Builder
    @Getter
    @Setter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public class classA {
      private String xxx = "yyy";
       private String abc = "123";
    }

   @Builder
   @Getter
   @Setter
   @ToString
   @NoArgsConstructor
   @AllArgsConstructor
public class classB {

     private String exp = "5";
     private final String exp1 = "2021";
     private final String exp2 = "5123456789012346";
     }

using Gson Pretty here and passing list of strings into the body.

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.