0

Similar question might be asked before on here, but I had no luck and I was wondering how to extract specific objects like user in from below json string and then construct an ArrayList. However, there is one twist, one of the property directly under Users is a random number, which can be anything!!!

Here is how my json string looks like:

<code>{
    "_links": {
    },
    "count": {
    },
    "users": {
        "123321": { //*Is a random number which can be any number
            "_links": {
            },
            "user": {
                "id": "123321",
                "name": "...",
                "age": "...",
                "address": ""
                ..
            }
        },
        "456654": {
            "_links": {
            },
            "user": {
                "id": "456654",
                "name": "...",
                "age": "...",
                "address": ""
                ...
            }
        }
        ...
    },
    "page": {
    }
}
</code>

The java object I would like to transform it to is:

@JsonIgnoreProperties(ignoreUnknown = true) // Ignore any properties not bound here
public class User {
    private String id;
    private String name;
    //setter:getter
}

Note: The transformation should only consider those two fields (id,name), and ignore the rest of the fields from the json response user:{} object.

Ideally, I would like to end up with a list like this:

List<User> users = resulted json transformation should return a list of users!!

Any idea how can I do this please ideally with Jackson JSON Parser/ or maybe GSON?

3
  • Possible duplicate of Ignoring new fields on JSON objects using Jackson Commented Nov 12, 2017 at 14:59
  • Well not really coz I’ve a random object name which can be anything! Commented Nov 12, 2017 at 15:01
  • Also that answer doesn't show how to construct List<User> list! Commented Nov 12, 2017 at 15:18

2 Answers 2

1

Since the user keys are random, you obviously can't map them to a named Java field. Instead, you can parse the top-level object as a map and the manually pull out the user objects.

public class UserWrapper {
    private User user;
    public User getUser() { return user; }
}

public class Root {
    private Map<String, UserWrapper> users;

    public List<User> getUsers() {
        List<User> usersList = new ArrayList();
        for (String key : map.keySet()) {
            UserWrapper wrapper = map.get(key);
            usersList.add(wrapper.getUser());
        }

        return userList;
    }
}

Root root = parseJson();
List<User> users = root.getUsers()

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

jolt transformer is your friend. Use shift with wildcard * to capture arbitrary node value and then standard mappers (Jackson /gson) .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.