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

  • SEARCH
  • Home
  • 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 719131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:36:25+00:00 2026-05-14T05:36:25+00:00

I am trying to build JSON from two fields. Say, I have a list

  • 0

I am trying to build JSON from two fields. Say, I have a list of object(party), and I only need to pass 2 items as JSON pair.

def list = getMyList() //it contains 2 party objects
partyTo = array {
    for (i in list) {
        x partyId: i.id
        y partyName: i.toString()          
    }
}

The JSON string is

{
    "partyTo": [
        {"partyId":12},
        {"partyName":"Ar"},
        {"partyId":9},
        {"partyName":"Sr"}
    ]
}

when I extract it at the client, it is treated as 4 objects. I wanted as 2 objects, with the below format.

{
    "partyTo": [
        {"partyId":12, "partyName":"Ar"},
        {"partyId":9 , "partyName":"Sr"}
    ]
}

I’m getting 4 objects, probably because I use an array to build JSON. I’m new to groovy and JSON, so not sure about the right syntax combinations. Any help highly appreciated.
thanks.

  • 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-05-14T05:36:26+00:00Added an answer on May 14, 2026 at 5:36 am

    You’re right in that the problem is with the construction of your array. To get the required output you need an array of maps, one map for each object, and to get the overall “partyTo” object you need to add that list to another map:

        def parties = [
                ["id":12 , "name":"Ar", "privateField": "a"],
                ["id":9 , "name":"Sr", "privateField": "b"]
        ]
    
        def toRender = parties.collect { party->
            ["partyId": party.id, "partyName":party.name]
        }
    
        def result = ["partyTo" : toRender]
        render result as JSON
    

    In other words, a Grails map turns into a JSON object and Grails arrays and lists become arrays in JSON

    If you always want to render your “party” objects like this you might consider using an ObjectMarshaller. More details in this post on rendering JSON using object marshallers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have been racking my brain trying to build a JSON array from
I'm trying to build a JSON object from a mysql query. The JSON structure
I'm trying to build an app that gets JSON from server and then shows
I'm trying to understand how to build a JSON object in JavaScript. This JSON
Trying to build dynamic output from json and using jq/template tmpl display rows/columns. Somehow
I am trying to get JSON data from a string that I have created
I am trying to build my first iOS app that parses json from a
I'm trying to build a server to handle JSON requests from an iOS application.
I'm trying to build a Rest JSON service with the WCF REST Service Template
I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but

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.