3

I am using rest assured to make an API post call and getting a response back in the body. I then need to take this response and select specific field values and store them as strings to be compared later against other string objects. I wrote jsonpath just fine to get the top level field values (like id, status, type, country, etc.) but when i have to get inside one of the objects within the json array that is returned I cant get the format correctly for the get() method.

Here is an example of the Json that is returned:

{
  "id": "ABC123",
  "status": "NEW",
  "type": "PERSONAL",
  "country": "United States",
  "totalBalances": {},
  "availableBalances": {},
  "fields": [
    {
      "fieldType": "mobilephone",
      "value": "14216904425",
      "fieldId": "personalMobileNumber"
    },
    {
      "fieldType": "email",
      "value": "user12345@work.com",
      "fieldId": "personalEmail"
    },
    {
      "fieldType": "STRING",
      "value": "John Doe",
      "fieldId": "individualName"
    }
  ]
}

Here is the json path I was trying to get formatted to fit into the get() method but i get an Illegal Argument exception everytime (java.lang.IllegalArgumentException: Invalid JSON expression) i try to make it work. Basically I need to identify the right object in the array and grab the proper field value. In this case that is the fieldId field and i want the field "value" value (John Doe) so that I can save that to a String object:

JsonPath pathToAccountName = response.jsonPath();
String accountName = pathToAccountName.get("fields[?(@.fieldId=='individualName')].value")

I used https://jsonpath.curiousconcept.com/ for the getting the VALID json path:

$.fields[?(@.fieldId=='individualName')].value

But I tried everything to convert it into something the get() method will accept and no luck. Scouring all the the posts here and the rest assured technical docs hasnt helped either.

D-Money
  • 137
  • 1
  • 12

2 Answers2

9

Rest Assured uses Groovy's Gpath. So your query could look like this:

JsonPath pathToAccountName = response.jsonPath();
String value = jsonPath.getString("fields.find { it.fieldId == 'individualName' }.value");

Here you can find some examples (it's about processing XML, but also applicable to JSON): http://groovy-lang.org/processing-xml.html

bhusak
  • 1,320
  • 1
  • 9
  • 19
-1

I need to know how to get this output -

JSON DOC - { "status": "E000", "customerId": "VjAxI2VhNzg5ZmJlLWIyNjAtNGZlOS1iZDNkLTdjMmU1MjA2ZmVhZA", "merchantId": "1", "cards": [ { "cardType": "DEBIT", "cardIssuer": "AXIS", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "HDFC Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "VISA", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "DEBIT", "cardIssuer": "Punjab National Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "MASTERCARD", "nickName": null, "expired": null, "cardMigrationStatus": "DONE", "tur": null, "category": null } ], "status_mssg": null }

I want this output - [ { "cardType": "DEBIT", "cardIssuer": "AXIS", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "ICICI Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "CREDIT", "cardIssuer": "HDFC Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null }, { "cardType": "DEBIT", "cardIssuer": "Punjab National Bank", "cardBrand": "MASTERCARD", "nickName": "", "expired": "false", "cardMigrationStatus": "OPEN", "tur": null, "category": null } ]

I am using this - jsonPathValidator.getString("$.cards[?(@.cardMigrationStatus == 'OPEN' && @.cardBrand == 'MASTERCARD')]");

but not getting the output.

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/31106379) – Procrastinator Feb 23 '22 at 07:56