Frank Provost Frank Provost - 1 month ago 15
Java Question

Parse incoming JSON and remove wrapping boilerplate

Within my program I'm calling an api which returns a list of users. However the result is wrapped in boilerplate like this.

{
"d": {
"result" : [ ... here the actual lsit ... ]
}
}


I want to parse the result, but i cannot find an easy way to remove the "d" and "result" from my incoming json string

I started with creating my own class for a staging user entity, because the resultset does not exactly match my users entity

class StagingUser {
// same props as api response per user
}


My thought was then to parse the useres like this

private List<StagingUser> parseResult(@RequestBody List<StagingUser> stagingUsers) {
return stagingUsers;
}


However it would require me to get rid of the boilerplate result

Answer

You can use gson library. gson-2.8.0.jar

Ignoring "D":

enter image description here

Maven:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>

StagingUser:

public class StagingUser
{
    private String hi;
    private String name;
    private String lastName;

    public StagingUser()
    {

    }

    public String getHi ( )
    {
        return hi;
    }

    public void setHi ( String hi )
    {
        this.hi = hi;
    }

    public String getName ( )
    {
        return name;
    }

    public void setName ( String name )
    {
        this.name = name;
    }

    public String getLastName ( )
    {
        return lastName;
    }

    public void setLastName ( String lastName )
    {
        this.lastName = lastName;
    }


    @Override
    public String toString ( )
    {
        return "StagingUser [hi=" + hi + ", name=" + name + ", lastName=" + lastName + "]";
    }


}

JsonTest:

This is getting a json string format, parsing ignoring d wrapper, and then set the attributes to StagingUsers. Is printing everything for informational purposes

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class JsonTest
{

    public static void main ( String [ ] args )
    {
        String jsonInString = "{ \"d\": { \"result\" : [ \"hello\", \"Frank\", \"Provost\" ] } }";

        System.out.println ( "Printing JSON String" );
        System.out.println ( jsonInString );
        System.out.println (  );

        JsonParser parser =  new JsonParser();
        JsonObject jsonObject = parser.parse(jsonInString).getAsJsonObject().get("d").getAsJsonObject();
        JsonArray jsonArray =  jsonObject.get("result").getAsJsonArray();

        System.out.println ( "Printing result elements" );
        for( JsonElement property : jsonArray)
        {
            System.out.println ( property.getAsString ( ));
        }
        System.out.println (  );


        StagingUser user = new StagingUser ( );
        user.setHi ( jsonArray.get ( 0 ).getAsString ( ) );
        user.setName ( jsonArray.get ( 1 ).getAsString ( ) );
        user.setLastName ( jsonArray.get ( 2 ).getAsString ( ) );

        System.out.println ( "Printing POJO: " );
        System.out.println ( user.toString ( ) );
    }

}

Output:

Printing JSON String

{ "d": { "result" : [ "hello", "Frank", "Provost" ] } }

Printing result elements

hello

Frank

Provost

Printing POJO:

StagingUser [hi=hello, name=Frank, lastName=Provost]