3

This is my current json:

{"name":"James",
    "child": {"id":1234,"name":"Ruth",
        "grandchild":{"id":1111,"name":"Peter"}
    }
}

I want to make it like this:

{"name":"James",
    "child": [{"id":1234,"name":"Ruth",
        "grandChild":[{"id":1111,"name":"Peter"}]
     }]
}

Below is the code:

def getParentJSON = {
    Json.obj(
        "name"->"James",
        "child"->getChildJson
    )
}

def getChildJSON = {
    Json.obj(
        "id"->"1234",
        "name"->"Ruth",
        "grandChild"->getGrandChildJson
    )       
}

def getGrandChildJSON = {
    Json.obj(
        "id"->"1111",
        "name"->"Peter"
    )       
}

I tried to use JsArray.append(getParentJSON). But it didn't worked.

Any help will be much appreciated.

Thanks

1 Answer 1

2

Use Json.arr:

def getParentJSON = {
  Json.obj(
    "name" -> "James",
    "child" -> Json.arr(getChildJSON)
  )
}

def getChildJSON = {
  Json.obj(
    "id" -> "1234",
    "name" -> "Ruth",
    "grandChild" -> Json.arr(getGrandChildJSON)
  )
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.