0

I have a JSON file with many objects and arrays and objects within the arrays.

I have a list of routes between a source and destination in the routes object accessible in the json path - data.offer.travels[1].routes. There are say 15 such routes objects.

Within each route object there is an array called legs whose length can be either 1, 2 or 3. I have written the method with a for loop to loop within routes object and return the first route index that had only 1 leg. But it gives an error.

Written a for loop to do this. Here is the code

public JSONObject PrepareProvBookingRequestBody() throws IOException, ParseException {

    File jsonExample = new File(System.getProperty("user.dir"),"\\JSONOutputFiles\\ThreeAdults_TwoCh_StdRet_PUB\\ThreeAdults_TwoCh_StdRet_PUB_JS.json");
    JsonPath jsonPath = new JsonPath(jsonExample);
    int routeindexOutbound=0;
    int routeindexInbound=0;
    List<Object> routesOutbound = jsonPath.getList("data.offer.travels[1].routes");
    int NoOfOutboundRoutes= routesOutbound.size();

    for (int i=0; i<NoOfOutboundRoutes; i++)
    {
        JsonArray legs = jsonPath.get("data.offer.travels[1].routes[i].legs");
        int NoOfLegs = legs.size();
        if (NoOfLegs==1) {
        routeindexOutbound=i;
        break;
     }

    }

String DepartureDate = jsonPath.getString("data.offer.travels[1].routes[routeindexOutbound].legs[0].service_schedule_date");

This is the error I get

java.lang.IllegalArgumentException: The parameter "i" was used but not 
defined. Define parameters using the JsonPath.params(...) function

Can someone kindly advice on where am I going wrong here?

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Mihir
  • 491
  • 5
  • 21

1 Answers1

1

You need define params with jsonPath.param("i",i).get ... This documentation with more example

Vlad Cheremisin
  • 141
  • 1
  • 11
  • Thanks Vlad for your inputs. That did resolve my issue. I have another question though. Where I check if the number of legs is 1, I want to return the value of the 'i' and I am struggling to return the value of 'i'. It says - The parameter "routeindexOutbound" was used but not defined. Define parameters using the JsonPath.params(...) function . routeindexOutbound is an int. All I want to return is the array index. Where I am going wrong? I changed to this line int NoOfLegs = jsonPath.param("i", i).getList("data.offer.travels[1].routes[i].legs").size(); – Mihir Jul 19 '19 at 10:22
  • You can store it in additional variable numberOfLegs = i and define it outside for statement – Vlad Cheremisin Jul 19 '19 at 10:31
  • @VladChermisin : How can I handle something like this: `String TariffCode = jsonPath.param("RouteIndex", RouteIndex,"BundlesIndex", BundlesIndex).getString("data.offer.travels[0].routes[RouteIndex].bundles[BundlesIndex].items[0].passenger_fares[0].tariff_code");` where I have multiple parameters. I wanted to use 2 parameters as its a nested structure I am dealing with. – Mihir Jul 24 '19 at 11:25
  • 1
    i'm not sure but it seems you need `jsonPath.param("RouteIndex", RouteIndex).param("BundlesIndex", BundlesIndex).getString("data.offer.travels[0].routes[RouteIndex].bundles[BundlesIndex].items[0].passenger_fares[0].tariff_code");` – Vlad Cheremisin Jul 24 '19 at 13:15
  • Thanks a lot Vlad! That worked! Thank you for your help! @Vlad – Mihir Jul 24 '19 at 15:20
  • https://stackoverflow.com/questions/57243315/updating-excel-with-values-from-rest-assured-json-response-using-apache-poi @Vlad can you help me with another aspect wrt Rest Assured please? – Mihir Jul 28 '19 at 20:02
  • No worries @Vlad, I found a solution to that issue. I have another problem that I seek you help - https://stackoverflow.com/questions/57270072/getting-element-value-from-jsonpath-whose-root-is-an-array – Mihir Jul 30 '19 at 11:27