Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:29:31+00:00 2026-06-01T01:29:31+00:00

I need to create a json message like this: { success:true, count:3, data: [

  • 0

I need to create a json message like this:

{
  success:true,
  count:3,
  data: [
    {id:1},
    {id:2},
    {id:3}
  ]
}

I have tried this

import groovy.json.*

def tasks = [1, 2,3]
def json = new JsonBuilder()

json{
  success(true)
  count(tasks.size())
  data {
    tasks.each {
      data(
        id: it        
      )
    }
  }
}

JsonOutput.prettyPrint(json.toString())

but it doesn’t work. Can somebody show me how to make it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:29:32+00:00Added an answer on June 1, 2026 at 1:29 am

    I’d recommend transforming the tasks list into a list of the form [[id: 1], [id: 2], [id: 3]] and then “jsonizing” it:

    import groovy.json.JsonBuilder
    
    def tasks = [1, 2, 3]
    def json = new JsonBuilder()
    
    json{
      success(true)
      count(tasks.size())
      data(tasks.collect {[id: it]})
    }
    
    println json.toPrettyString()
    

    I usually prefer generating the data structures in Groovy first (e.g. lists, maps) and then convert them to JSON, that way I usually have more control over the data that is going to be serialized and I don’t need to include logic on the serialization code.

    import groovy.json.JsonBuilder
    
    def tasks = [1, 2, 3]
    
    def data = [
      success: true,
      count: tasks.size(),
      data: tasks.collect {[id: it]}
    ]
    
    def json = new JsonBuilder(data)
    println json.toPrettyString()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create JSON objects from clojure maps that store things like clojure
I have this json object : { msg_id:134, message:Dick, comment:[ { com_id:9, comment:test, msg_id:134,
I need to pre-compress some very large html/xml/json files (large data dumps) using either
I have a JSON response from a web service that I need to be
I have a JSON string with the multiple instances of the following Name Message
i get this error Parser Error Message: Could not create type 'charts.lineChartData'. Source Error:
I have created a library which can download JSON data which is then placed
I need to create a list like List<String,Object> but I can't seem to do
I have followed this tutorial which uses jQuery UI to generate Facebook tokens like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.