1

I have some JSON coming in that's like this (data removed for privacy reasons.

{"user":{"id":"","email":"","first_name":"","last_name":""}, "STUFF I DONT WANT HERE"}

I wrote an inner wrapper class for it like this:

    public with sharing class WrapperUserJson {
        public String id;
        public String email;
        public String first_name;
        public String last_name;
    }

In a method I am doing something like WrapperUserJson data = (WrapperUserJson)JSON.deserialize(st, WrapperUserJson.class);

But it always returns null. I know for a fact that the JSON is being passed to it so I don't get it. What am I missing?

1 Answer 1

1

You're missing the top-level attribute user. You need a second wrapper class:

public class WrapperResponseJson {
    WrapperUserJson user;
}
public class WrapperUserJson {
    public String id;
    public String email;
    public String first_name;
    public String last_name;
}

Which you then feed into the deserialize method:

WrapperResponseJson data = (WrapperResponseJson)JSON.deserialize(st, WrapperResponseJson.class);

You can automatically generate code from JSON using json2apex, a great tool for quickly creating Apex from a given JSON source.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .