1

I have written a code, where I need to create a JSON format output from a "select * query". So far I am able to get the output like this, but I have to wrap it up inside "users"

{
  "displayName": "Tony Stark",
  "givenName": "Tony",
  "surname": "Stark"
},
{
  "displayName": "James Martin",
  "givenName": "James",
  "surname": "Martin"
}

I need something like this:

{
   "users": 
      [
            {
                  "displayName": "Tony Stark",
                  "givenName": "Toney",
                  "surname": "Stark"
            },
            {
                  "displayName": "James Martin",
                  "givenName": "James",
                  "surname": "Martin"
            }
      ]
}

Please help me out in this.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
SharadxDutta
  • 1,058
  • 8
  • 21
  • 3
    Please update the question with code where you are querying database and converting to json, – Smile Aug 11 '20 at 06:34
  • Does this answer your question? [How to create correct JSONArray in Java using JSONObject](https://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject) – wovano Aug 11 '20 at 15:25

2 Answers2

1

Yes it's possible if you create a model class named Users, map it properly along it with it's attributes so it will show exactly as shown by you in the screenshot. Attaching reference model class.

enter image description here

Smile
  • 3,832
  • 3
  • 25
  • 39
Ben Rahul
  • 11
  • 2
0

It would be easy if add a wrap class and convert it to json

import java.util.List;

public class UserWrap {
    private List<User> users;

    public UserWrap(List<User> users) {
        this.users = users;
    }
}