1

I'm trying to Test my Rest Web Service class. I want to simulate the process by taking a JSON and deserialize it to a class - Instead to create all the data by my self.

But for some reason - all my values after deserialization is null - Can someone please help me to understand why?

Json

{
  "ContactJs": {
    "another_Id": "cjfv5g33k61ls7jdlf4353kjg",
    "first_name": "2",
    "last_name": " 2 ",
    "first_name_english": "Tests2",
    "last_name_english": "Tests22",
    "email": "[email protected]",
    "id_number": "01h11162t31f215",
    "father_name": "fh",
    "passport_number": "023117735",
    "passport_issued_country": "USA",
    "birthdate": "30/11/2001",
    "mobile_phone": "05871336597",
    "gender": "Female",
    "contact_birth_country": "USA",
    "country_origion_mother": "USA",
    "country_origion_father": "USA",
    "hed_ethnicity": "01",
    "other_ethnicity": "fgh",
    "immigrated_year": "2000",
    "demographic_city": "123",
    "no_promotion_interactions": true,
    "international_area_code": "+972",
    "residency_type": "1"
  }
}

Class

public class ContactJs
{
    public String another_Id;
    public string first_name;
    public string last_name;
    public string first_name_english;
    public string last_name_english;
    public string email;
    public string id_number;
    public string passport_number;
    public String passport_issued_country;
    public String birthdate;
    public string mobile_phone;
    public String gender;
    public string contact_birth_country;
    public string country_origion_mother;
    public string country_origion_father;
    public string hed_ethnicity;
    public string other_ethnicity;
    public string immigrated_year;
    public string demographic_city;
    public Boolean no_promotion_interactions;
    public String international_area_code;
    public String residency_type;
    public String father_name;
}

Deserialize

String jso = '{"ContactJs": { "another_Id": "cjfv5g33k61ls7jdlf4353kjg", "first_name": "2", "last_name": " 2 ", "first_name_english": "Tests2", "last_name_english": "Tests22", "email": "[email protected]", "id_number": "01h11162t31f215", "father_name": "fh", "passport_number": "023117735", "passport_issued_country": "ISR", "birthdate": "30/11/2001", "mobile_phone": "05871336597", "gender": "Female", "contact_birth_country": "ISR", "country_origion_mother": "USA", "country_origion_father": "ISR", "hed_ethnicity": "01", "other_ethnicity": "fgh", "immigrated_year": "2000", "demographic_city": "123", "no_promotion_interactions": true, "international_area_code": "+972", "residency_type": "1"}}';
ContactJs data = (ContactJs) JSON.deserialize(jso, ContactJs.class);

System.debug(data);

Output

ContactJs:[another_Id=null, birthdate=null,
contact_birth_country=null, country_origion_father=null,
country_origion_mother=null, demographic_city=null, email=null,
father_name=null, first_name=null, first_name_english=null,
gender=null, hed_ethnicity=null, id_number=null, immigrated_year=null,
international_area_code=null, last_name=null, last_name_english=null,
mobile_phone=null, no_promotion_interactions=null,
other_ethnicity=null, passport_issued_country=null,
passport_number=null, residency_type=null]

1 Answer 1

5

Just remove ContactJs, You don't need to include class name.

This should work

String jso = '{ "another_Id": "cjfv5g33k61ls7jdlf4353kjg", "first_name": "2", "last_name": " 2 ", "first_name_english": "Tests2", "last_name_english": "Tests22", "email": "[email protected]", "id_number": "01h11162t31f215", "father_name": "fh", "passport_number": "023117735", "passport_issued_country": "ISR", "birthdate": "30/11/2001", "mobile_phone": "05871336597", "gender": "Female", "contact_birth_country": "ISR", "country_origion_mother": "USA", "country_origion_father": "ISR", "hed_ethnicity": "01", "other_ethnicity": "fgh", "immigrated_year": "2000", "demographic_city": "123", "no_promotion_interactions": true, "international_area_code": "+972", "residency_type": "1"}';
ContactJs data = (ContactJs) JSON.deserialize(jso, ContactJs.class);

System.debug(data);

15:21:30:003 USER_DEBUG [32]|DEBUG|ContactJs:[another_Id=cjfv5g33k61ls7jdlf4353kjg, birthdate=30/11/2001, contact_birth_country=ISR, country_origion_father=ISR, country_origion_mother=USA, demographic_city=123, [email protected], father_name=fh, first_name=2, first_name_english=Tests2, gender=Female, hed_ethnicity=01, id_number=01h11162t31f215, immigrated_year=2000, international_area_code=+972, last_name= 2 , last_name_english=Tests22, mobile_phone=05871336597, no_promotion_interactions=true, other_ethnicity=fgh, passport_issued_country=ISR, passport_number=023117735, residency_type=1]

0

You must log in to answer this question.

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