0

I'm using restassured.path.json.JsonPath extractor

From the below response I would like to extract the customerProductIdentifier where highLevelProduct is HSI,

{
    "recordsChoice": [
        {
            "listedName": "EMA WATSON",
            "address": {
                "streetAddress": "",
                "addressLine1": "1900 SKY FLOWER CT",
                "addressLine2": "LAS VEGAS, NV"
            },
            "recordIdentifier": "310290154",
            "accountStatus": "Open"
        }
    ],
    "rxSessionIdentifier": "33062565",
    "accountInformation": {
        "billingIdentifier": "310290154",
        "billingName": "HANDY SHOWROOM",
        "billingAddress": {
            "streetAddress": "",
            "addressLine1": "1900 SKY FLOWER CT",
            "addressLine2": "LAS VEGAS, NV"
        },
        "customerType": "Consumer",
        "customerSubType": "Regular",
        "allServicesDeniedForNonPayment": false,
        "accountStatus": "Open",
        "isMilitaryAccount": false,
        "primaryClassOfServiceCode": "N.A",
        "classOfService": "N.A",
        "usocsOnAccount": [
            "IL",
            "WA"
        ],
        "hasPrism": true,
        "isMadisonRiverCustomer": false,
        "customerDateTime": "01/02/2019 03:58",
        "customerTimeGMTOffset": "-08:00",
        "customerTimeZoneAbbreviation": "US/Pacific",
        "legacyDispatchTerritory": "QCENTER",
        "billingSystem": "QCENTER",
        "smsDigitalDialogPreferences": {
            "repairSubscription": false,
            "defaultContact": false,
            "invalidFlag": false,
            "billingSubscription": false,
            "orderSubscription": false
        },
        "emailDigitalDialogPreferences": {
            "subscribedEmail": "[email protected]",
            "repairSubscription": true,
            "defaultContact": true,
            "marketingSubscription": true,
            "billingSubscription": true,
            "orderSubscription": true
        }
    },
    "serviceLines": [
        {
            "centralOffice": "LVSSNV",
            "locationTimeZone": "US/Pacific",
            "serviceAddress": {
                "streetAddress": "",
                "addressLine1": "1210 STRATOSPHERE",
                "city": "LAS VEGAS",
                "stateProvince": "NV",
                "postalCode": "89123-3959",
                "countryCode": "USA"
            },
            "hasGPON": false,
            "accessTech": "ADSL2PB",
            "hasVectoring": false,
            "isNIC": false,
            "isClickConverted": true,
            "isPrivateCarriage": false,
            "isVISP": false,
            "hasProfile17a": false,
            "hasInsideWireMaintenancePlan": true,
            "serviceTnOrCktId": "8861061786",
            "customerProducts": [
                {
                    "customerProductIdentifier": "314417931",
                    "ossProfile": {
                        "ossProfileName": "TraditionalEnsembleClick",
                        "billingSource": "Ansemble",
                        "inventorySource": "MARTENS",
                        "ticketingSystem": "OneRemedy",
                        "dispatchSystem": "Click"
                    },
                   "usoc": "WA",
                    "otherUsocs": [],
                    "highLevelProduct": "HSI",
                    "isActive": true,
                    "activationDate": "12/12/1985 00:00",
                    "productStatus": "Active",
                    "hasAtEaseFeature": false,
                    "purchasedSpeedDownKbps": "10000",
                    "purchasedSpeedUpKbps": "768",
                    "connectionType": "IPOE",
                    "isCentralizedBRAS": "false",
                    "sourceOfOpenSessionInfo": "BRAS"
                },
                {
                    "customerProductIdentifier": "314417930",
                    "ossProfile": {
                        "ossProfileName": "TraditionalEnsembleClick",
                        "billingSource": "Ensemble",
                        "inventorySource": "FCAS",
                        "ticketingSystem": "JACKS",
                        "dispatchSystem": "Click"
                    },
                    "usoc": "IL",
                    "otherUsocs": [],
                    "highLevelProduct": "VOICE",
                    "isActive": true,
                    "activationDate": "09/21/2014 00:00",
                    "productStatus": "Active",
                    "hasAtEaseFeature": false
                },
                {
                    "customerProductIdentifier": "314417929",
                    "ossProfile": {
                        "ossProfileName": "TraditionalEnsembleClick",
                        "billingSource": "Ansemble",
                        "inventorySource": "FCAS",
                        "ticketingSystem": "JACKS",
                        "dispatchSystem": "Click"
                    },
                    "usoc": "IL",
                    "otherUsocs": [],
                    "highLevelProduct": "CTL_PRISM",
                    "isActive": true,
                    "activationDate": "09/21/2014 00:00",
                    "productStatus": "Active",
                    "hasAtEaseFeature": false,
                    "connectionType": "IPOE",
                    "isCentralizedBRAS": "false",
                    "sourceOfOpenSessionInfo": "BRAS"
                }
            ]
        }
    ],
    "apiResponse": {
        "code": 0,
        "type": "S",
        "message": "Success"
    }
}

I have this code to extract the billingName and rxSessionIdentifier from the response, but I'm not sure how to extract the customerProductIdentifier where highLevelProduct is HSI

extract().response();

        String responseString=res.asString();
        System.out.println(responseString);

         //To grab the particular parameters from the response
        JsonPath js = new JsonPath(responseString);
        // To extract the billing name
        String billingName = js.get("accountInformation.billingName");
        System.out.println("Billing Name of the customer is " +billingName);

        // To extract the session id
        String rxSessionIdentifier = js.get("rxSessionIdentifier");
        System.out.println("Corresponding RxSessionId is " +rxSessionIdentifier);
2
  • you need to write a loop to iterate over the serviceLines.customerProducts to find the record you're looking for
    – ernie
    Commented Feb 8, 2019 at 18:23
  • I tried doing that with the below code but I have an error customerProducts cannot be resolved or is not a field @Test private void HSIProductID (JsonPath js){ for(int i=0;i<2;i++){ if(js.get(serviceLines.customerProducts[i].highLevelProduct=="HSI") != null) { System.out.print(js.getString("serviceLines.customerProducts[i].customerProductIdentifier")); } } } Commented Feb 11, 2019 at 4:31

1 Answer 1

1

Use the following jsonpath query:

serviceLines[0].customerProducts.findAll{product -> product.highLevelProduct == 'HSI'}.customerProductIdentifier

Test:

System.out.println(js.get("serviceLines[0].customerProducts.findAll{product -> product.highLevelProduct == 'HSI'}.customerProductIdentifier").toString());

Note, that it will return list of identifiers in case your JSON contains several objects meeting your requirement.

5
  • I am getting below error FAILED: Ensemble java.lang.IllegalArgumentException: Invalid JSON expression: Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 59. rviceLines[0].customerProducts[?(@.highL ^ 1 error at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.codehaus.groovy.reflection.CachedConst Commented Feb 10, 2019 at 19:12
  • Are you sure you're using the query I provided? As per your exception there is a parsing issue on position 59 where the parser encounters [ character. But in my query there is H on that position.
    – Alexey R.
    Commented Feb 11, 2019 at 10:34
  • Yes I am sure I am using the right path, The JsonPath shipped with rest assured is not using the same syntax as the json-path pseudo standard. It uses gpath. See documentation in the link github.com/rest-assured/rest-assured#Example_1_-_JSON Commented Feb 15, 2019 at 4:58
  • @MohammedAZahid check my update
    – Alexey R.
    Commented Feb 15, 2019 at 11:21
  • 1
    Alexey R, you are awesome . That worked fine. Just that the output got printed within two square brackets. I wrote two lines of code just to remove them. Commented Feb 27, 2019 at 11:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.