'I need to send the request correctly, but I don't know how to get the required values from objects

How to get a new list from the list of objects? I need a new list of objects to POST request

this list of objects i get from response:

{ 
 "success": true,
  "body": {
      "users": [
        {
            "type": "unknown",
            "data": [
                {
                    "id": "8",
                    "firstName": "Jackson",
                    "lastName": "Baker",
                    "group": "false"
                },
                {
                    "id": "11",
                    "firstName": "Charlotte",
                    "lastName": "Garcia",
                    "group": "false"
                },
                {
                    "id": "7",
                    "firstName": "Henry",
                    "lastName": "Thompson",
                    "group": "false"
                },
                {
                    "id": "24",
                    "firstName": "Elijah",
                    "lastName": "Miller",
                    "group": "false"
                  }
                
              ]
           }  
        ]
      }
   }

I need to form a new object from response:

{ 
  "success": true,
    "body": {
       "users": [
        {
            "type": "unknown",
            "data": [
                {
                    "id": "8",                   
                    "group": "false"
                },
                {
                    "id": "11",
                    "group": "false"
                },
                {
                    "id": "7",
                    "group": "false"
                },
                {
                    "id": "24",
                    "group": "false"
                }
                
              ]
           }  
        ]
      }
   }

This new list of object I need to post a request. I have a JSON Schema, but i don't know how i can make this structure. My problem is that I can't get the values from fields. I'm using the path "body.users.data.find {it.id} .id" but it finds the id array but I need a users list with values from two fields.



Solution 1:[1]

As the users node and data node are all list type, you need use two loop:

import groovy.json.JsonSlurper
// text is your json response text
def result = new JsonSlurper().parseText(text)
result.body.users.each{it.data.each{it.remove("firstName");it.remove("lastName")}}
// result is the what you wanted
println(result)   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xiao