4

I have the Json string coming from DB field and I want that to be converted to Apex. I tried the below code but that only returns the first instance in json string.

 { 
 "firstname":"abc", 
 "Lastname": "xyz",  
 }, 
 { 
  "firstname":"abc1", 
  "Lastname": "xyz1",  
 }

Apex class that i am using is

public class JSON2Apex {

    public String firstname;
    public String Lastname;



    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

And how i am calling is

JSON2Apex obj = JSON2Apex.parse(json);

And it returns only

JSON2Apex:[firstname= abc, Lastname=xyz]

Can anyone suggests where am i doing wrong.I know we need to create List to hold all items but not sure where and how.

  • 1
    your json is invalid, validate it using jsonlint.com if you have to pass an array enclose inside [ .. ] – Rao Nov 23 '16 at 19:27
5

That's because it's not properly formed JSON. You're missing the outer square brackets ([]) that indicate it's a list.

Correct

[
    {
        "firstName": "John",
        "lastName": "Doe"
    },
    {
        "firstName": "Jane",
        "lastName": "Doe"
    }
]

Incorrect

{
    "firstName": "John",
    "lastName": "Doe"
},
{
    "firstName": "Jane",
    "lastName": "Doe"
}

I don't think JSON2Apex will give you a correct class to serialize into when you just have a List. What you really want would be to deserialize into a List<JSON2Apex>.

public class JSON2Apex {
    public String firstname, lastname;
    public static List<JSON2Apex> parse(String json) {
        return (List<JSON2Apex>)System.JSON.deserialize(json, List<JSON2Apex>.class);
    }
}
  • I actually confirmed that. You need to have an outer parameter for it to parse into a list. I wonder if this is a bug? – sfdcfox Nov 23 '16 at 19:35
  • @sfdcfox confirmed what? I think it's just a mistaken assumption that the payload will be in object form instead of array form. I would argue the "bug" is just over-reliance on tools without thinking critically about the output. – Adrian Larson Nov 23 '16 at 19:57
2

You need to make your JSON an array:

 [{ 
 "firstname":"abc", 
 "Lastname": "xyz"  
 }, 
 { 
  "firstname":"abc1", 
  "Lastname": "xyz1"  
 }]

And then parse it as an array:

public static List<JSON2Apex> parseList(String json) {
    return (List<JSON2Apex>) System.JSON.deserialize(json, List<JSON2Apex>.class);
}

Note: It appears that even in array syntax, JSON2Apex doesn't "get it right", as it still produces the same code as what you've posted in your original question. This is one of those cases where you need to know enough about JSON to be able to figure out what went wrong.

Alternatively, given a choice of changing your JSON, you could write it as follows:

{ "records": [{ 
 "firstname":"abc", 
 "Lastname": "xyz"  
 },
 { 
 "firstname":"abc", 
 "Lastname": "xyz"  
 }
]}

Which produces the following JSON2Apex code:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
public class JSON2Apex {

    public List<Records> records;

    public class Records {
        public String firstname;
        public String Lastname;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}
  • ok let me quickly try that.If that helps in my case. – Coder Nov 23 '16 at 20:07
  • I used the same concept and it returned the the output as (JSON2Apex: [ firtname=abc, Lastname=xyz ], JSON2Apex: [ firtname=abc, Lastname=xyz ]) Which i assigned to List<JSON2Apex>, when i try to access the same it gives me empty list. – Coder Nov 24 '16 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.