Skip to main content

I'm new to Apex . I have JSON data I need to convert to JSON Generator.

 

JSON data

 

#API  #Apex  #Json  #Salesforce  #REST API  #Trailhead

Contact app = [select Id, Name from Contact ];

{

"CustomerID": 0,

"CustomerType": 1,

"Name": "callum, john", (app.Name,app.LastName) like this i need in JSON Generator.

"BranchID": 1,

"CustomerAddress": [

{

"CustomerAddressID": 0,

"CustomerID": 0,

"Address": {

"Address1": "Test",

"TownCityID": 41037,

"CountyID": null,

"StateID": 41036,

"CountryID": 4,

"Country": "Australia"

},

"AddressTypeID": 1,

"DateMovedIn": "11/09/2019",

"IsPrimary": false

}

],

"Persons": [

{

"PersonID": 0,

"Title": 1,

"Gender": 0,

"ForenamePartOne": "callum",

"ForenamePartTwo": null,

"ForenamePartThree": null,

"Surname": "john",

"DateOfBirth": "11/09/1970",

"SocialSecurityNumber": "35",

"telephone1": "",

"telephone2": "",

"telephone3": "6598307315"

}

],

"CorrespondenceName": "callum, john",

"DateJoined": "11/09/2019"

}

6 respostas
  1. 20 de jul. de 2021, 17:40

    Hello @Meenu sweety,

    Actually in your code

    At start take a String and put value as app.FirstName +' ,' + app.LastName and use that for Name 

    gen.writeStringField('BranchID', '1');

    gen.writeStartObject(); // This line should not be present

    gen.writeFieldName('CustomerAddress');

    gen.writeObject(null); // Instead of this you should use gen.writeStartArray()

    and use gen.writeStartObject(); at the next line and end them accordingly also do same for "Persons"

0/9000