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
naveen reddy 19naveen reddy 19 

How to convert JSON string to required JSON string?

Hi ,
  
 I'm trying to parse Account records to JSON string. But I'm getting unnecessary data like attributes(type,url) that is not required in String.

Can any one pls help me in how to parse the string to required format.
List<Account> accnts=[Select Name,Phone From Account];
String s=JSON.serialize(accnts);
The resulting output is in below fromat..
{
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v34.0/sobjects/Account/00128000002trGGAAY"
  },
  "Name" : "GenePoint",
  "Phone" : "(650) 867-3450",
  "Id" : "00128000002trGGAAY"
}


My requirement is to generate resulting String as below JSON string format.
 
{
  "Name" : "GenePoint",
  "Phone" : "(650) 867-3450",
  "Id" : "00128000002trGGAAY"
}

So that I can minimize the data to to be sent the the client system.

Any help is really appreciated. Thanks in advance.

Regards,
Naveen.
Best Answer chosen by naveen reddy 19
Shailendra Singh ParmarShailendra Singh Parmar
Hi Naveen,
You can try Json Generator class to build json string which is 2nd way to create json in apex. Here is link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsongenerator.htm

Thanks!

All Answers

Shailendra Singh ParmarShailendra Singh Parmar
Hi Naveen,
You can try Json Generator class to build json string which is 2nd way to create json in apex. Here is link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsongenerator.htm

Thanks!
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Create below wrapper class
 
public class AccountWrapper
 {
  public String Name {get;set;}
  public String Id {get;set;}
  public String Phone {get;set;}
  public AccountWrapper(String name,String id,String phone)
  {
    this.Name =name;
    this.Id =id;
    this.Phone =phone;
  }
 }

And then modify your class like this 
List<Account> accnts=[Select Name,Phone From Account];
if(accnts.size() > 0)
{
AccountWrapper obj = new AccountWrapper(accnts[0].name , accnts[0].id ,accnts[0].phone );
String JSONBody = JSON.serialize(obj ); 
}

NOTE:- I created this code for single record. Can can create the array for warpper class. I created this code in notepad may be you will get some sytex error.

Please let us know if this will help you

Thanks,
Amit Chaudhary

 
naveen reddy 19naveen reddy 19
Thanks a lot Shailendra and Amit. Both the solutions helped me. 

Please let me know how can I mark both answers  as best answers..
Ajay K DubediAjay K Dubedi
Hi,
you can use a replace method to replace the unwanted data with a white space like below :

s.val().replace(/\\/g, ''));

in my case i was have to take a string to the page as json but i was getting these term extra so in java script i used this method replace().