0

I use this code to build an array of employees:

render(((new JsonBuilder())
        {
            'all' employees.collect { Employee employee ->
                [
                        id: employee.id,
                        name: employee.firstName + " " + employee.lastName
                ]
            }.sort() { it.name }
        }) as JSON)

the result is:

"{
    all: [
       {id: 1, name: "balh blah"},
       ...
    ]
}"

but I want a json of an array without the "all" field, like this:

"[
       {id: 1, name: "balh blah"},
       ...
 ]"

How can I achieve that?

Thanks for help!

1
  • Have you tried removing the 'all' part (line 3)? Commented Nov 30, 2013 at 0:57

1 Answer 1

2

You don't really need a JSonBuilder, if you have what you need in a collection, just return the collection as JSON.

def employee = Employee.findAll()
render employee as JSON

UPDATE: tried myself with the following code:

def  userList = User.list(params)
def all = userList.collect {User user ->
  [id: user.id,
  name : user.firstName + " " + user.lastName]
}.sort() { it.name }

render all as JSON
Sign up to request clarification or add additional context in comments.

2 Comments

as you can see in my code I customize the structure of the object, that why I'm using json builder. what you did wont help me to customize the object and I want to use json builder and not the register marsheler
@Noampz, just updated my post with the code that worked for me.

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.