0

I'm trying create a function in which I pass a json object from JsonSlurper and a string which contains json object located in the original. If it does, it returns true or false if the elements count condition is satisfied. For example:

myJson:

{
  "Errors": [],
  "Loans": [
    {
      "Applications": [
        {
          "id": 1,
          "name": "test"
        }
      ]
    },
    {
      "Applications": [
        {
          "id": 2,
          "name": "test3"
        },
        {
          "id": 3,
          "name": "test3"
        }
      ]
    }
  ]
}

My method would get the json array as follows:

def myJson = new JsonSlurper().parseText(receivedResponse.responseBodyContent)
def result = verifyElementsCountGreaterThanEqualTo(myJson, "Loans[0].Applications[1]", 3)

Is there such a library that can do this for me?

I've tried myJson["Loans[0].Applications[1]"] to get the Json Object so I can get the size, but the result is null.

usr4896260
  • 1,427
  • 3
  • 27
  • 50

3 Answers3

2

How about the following? And it is trivial, I guess.

Loans is a list where you get multiple Applications. Just pass the index of the Applications.

def json = new groovy.json.JsonSlurper().parseText(jsonString)
//Closure to get the particular Loan
def getLoanAt = { json.Loans[it]}
//Call above closure as method to print the 2nd Applications
​println getLoanAt(1)​

In case if you want to print all loan applications, here you do not need closure at all:

json.Loans.each {println it}​

Here is online demo for quick test.

In case, if you want a loan application by Id, use below:

//To get all loan application by Id
def getApplicationById = {id -> json.Loans.Applications.flatten().find{id == it.id}}
println getApplicationById(3)

Quick demo of the above.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Is it possible to use a `string` to get the data? Even with some other library? Ideally, would want something like `json."Loan.Applications[1]"` where `"Loan.Applications[1]"` would be a string parameter in my method. – usr4896260 Aug 29 '18 at 02:17
  • @usr4896260, there are 3 approaches mentioned in the above solution. Can't you afford / feasible to apply any of those? – Rao Aug 29 '18 at 02:39
  • What exactly you want to extract from above data? The above provides what you have mentioned i.e., `getLoanAt` is similar to (just named it appropriately) instead of `myMethod`. – Rao Aug 29 '18 at 02:43
  • I've updated the description. Basically I want to call a method like this `verifyElementsCountGreaterThanEqualTo(myJson, "Loans.Applications[1]", 3)` The requirement is that "Loans.Applications[1]" is the path to the json object. It must be a string so I can reuse the function across different json results. – usr4896260 Aug 29 '18 at 03:04
1

You can try to convert your json to java object Map to be accurate, after that you can get the Loans as an object ArrayList.

def myJson = new JsonSlurper().parseText("{\"Errors\": [], \"Loans\": [{\"id\": 1}, {\"id\": 2}]}");
def loansList = myJson.Loans// ArrayList
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
  • That was my current approach. However, my actual json object contains about 50 or so fields, each with its own nesting of objects/arrays. I was looking for a function that could help make my search for such properties dynamic. I revised my description. – usr4896260 Aug 28 '18 at 22:08
0

After a lot of searching, I was able to find a solution with the rest assured api.

I'm able to use a string for the path I'm looking for in the Json object as follows:

import io.restassured.path.json.JsonPath as JsonPath

def myJson = "{'Errors':[],'Loans':[{'Applications':[{'id':1,'name':'test'}]},{'Applications':[{'id':2,'name':'test3'},{'id':3,'name':'test3'}]}]}"    
def applicationData = JsonPath.with(myJson).get("Loans[0].Applications[1]")
def applicationsListData = JsonPath.with(myJson).get("Loans[0].Applications")
usr4896260
  • 1,427
  • 3
  • 27
  • 50