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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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();
You can store it in additional variable numberOfLegs = i and define it outside for statement
@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.
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");
Thanks a lot Vlad! That worked! Thank you for your help! @Vlad
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.