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
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)
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With