function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya manohardivya manohar 

json file issue

friends
I have a json file , I want to use deserializeuntyped method to understand it.

public class Jsonexample5
{
   public string result{get;set;}
   
   public Jsonexample5()
   {
     string jsonInput='{"response":{"count":1,"benchmark":0.22567009925842,"requests":[{"request":{"id":537481,"image_thumbnail":"","title":"Request for new bin(s) - residential","description":"Propmain ref  3234-1114","status":"submitted","address":"36 Pine Tree Close","location":"Peterborough, England","zipcode":"PE1 1EJ","user":"","date_created":1417173208,"count_comments":0,"count_followers":0,"count_supporters":0,"lat":52.599967,"lon":-0.233482,"user_follows":0,"user_comments":0,"user_request":1,"rank":"0"}}],"status":{"type":"success","message":"Success","code":200,"code_message":"Ok"}}}';
       
     Map<String,Object> jsmap = (Map<String,Object>)JSON.deserializeUntyped(jsonInput);
     system.debug('==>'+jsmap);
 
   }
}

I have 2 things to clarify
a) when I execute the above in developer console I get the deserialized format but it does not include attributes after location.
    i.e atrributes like zipcode , user , date_created , count_comments  etc are missing.

    The same input json string when executed in    http://codebeautify.org/jsonviewer#  gives all the attributes.

    I want to know why is this happening. Am I missing something:?


b) In real time scenario , do we use third pary tools to generate classes for json input or we have to manually write our own wrapper classes?

thanks
divya manohar
 
Best Answer chosen by divya manohar
Abu HashimAbu Hashim
Hey Divya,

1) when I try this sample code, the fields which you were mentioning like date_created,count_comments are coming as part of deserialization of json file.
kindly have a look at the below response which i've got,
08:24:16:046 USER_DEBUG [4]|DEBUG|==>{response={benchmark=0.22567009925842, count=1, requests=({request={address=36 Pine Tree Close, count_comments=0, count_followers=0, count_supporters=0, date_created=1417173208, description=Propmain ref  3234-1114, id=537481, image_thumbnail=, lat=52.599967, location=Peterborough, England, ...}}), status={code=200, code_message=Ok, message=Success, type=success}}}

Still, the response is there and due to debug limit it's getting truncated. It doesn't mean that response is not there. If we map correctly to internal salesforce fields or wrapper variables, we can extract the remaining fields as well.

2) Yes, you need to have SF object in order to capture the data which we will get in Deserialization of the json to map the corresponding attributes to the fields of object. Otherwise, Wrapper would also work for temp usage.

Hope this answers your question.

Choose as BEST answer if it has answered your question.

Thanks,
Abu
 

All Answers

Abu HashimAbu Hashim
Hey Divya,

1) when I try this sample code, the fields which you were mentioning like date_created,count_comments are coming as part of deserialization of json file.
kindly have a look at the below response which i've got,
08:24:16:046 USER_DEBUG [4]|DEBUG|==>{response={benchmark=0.22567009925842, count=1, requests=({request={address=36 Pine Tree Close, count_comments=0, count_followers=0, count_supporters=0, date_created=1417173208, description=Propmain ref  3234-1114, id=537481, image_thumbnail=, lat=52.599967, location=Peterborough, England, ...}}), status={code=200, code_message=Ok, message=Success, type=success}}}

Still, the response is there and due to debug limit it's getting truncated. It doesn't mean that response is not there. If we map correctly to internal salesforce fields or wrapper variables, we can extract the remaining fields as well.

2) Yes, you need to have SF object in order to capture the data which we will get in Deserialization of the json to map the corresponding attributes to the fields of object. Otherwise, Wrapper would also work for temp usage.

Hope this answers your question.

Choose as BEST answer if it has answered your question.

Thanks,
Abu
 
This was selected as the best answer
James LoghryJames Loghry
Divya, 

Please take a look at http://json2apex.heroku.com. It's a heroku app that will generate an Apex class based on your JSON format.  You can then use that Apex class to easily serialize to or deserialize from JSON.

 
divya manohardivya manohar
Hello all
I used   http://json2apex.heroku.com .to generate apex class as follows

public class Jsonexample1
{
    public String Name;
    public Object course;
    public Integer Age;
             
    public static Jsonexample1 parse(string jsonInput)
    {
      return (Jsonexample1) System.JSON.deserialize(jsonInput, Jsonexample1.class);
    }
 }
In developer console I am giving as  

string jsonInput='{"Name":"Henry","course":"Mathematics","Age":20}';    
system.debug('==>'+Jsonexample1.parse(jsonInput));


I am getting error : System.JSONException: Apex Type unsupported in JSON: Object

Can pls explain abt error:?

divya
divya manohardivya manohar
Hello
sorry, guys full marks to ur previous posts.

Regd my latest post, the error comes because I have given public Object course which is an unsupported type in json.
so I changed as :  public   String course  and it worked.

divya