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
VenkatVenkat 

Create JSON for wrapper class

Hi 

I am using code like below

@RestResource(urlMapping='/test/*')
global with sharing class ClassA
{
      @HttpPost
       global static void createSomething(Wrapper1 req)
        {
              System.debug('###stringVar1 :' + req.stringVar1);
              System.debug('###stringVar2:' + req.stringVar2);
              System.debug('###Wrapper2:' + req.Wrapper2);
             Object__c obj = new Object__c();
             obj.fld1 = req.stringVar1;
             obj.fld2 = req.stringVar2; insert obj;
             List<Object2__c> surveyQuestionsRespList = new List<Object2__c>();
             for(Wrapper2 question : req.Wrapper2)
             {
                      Object2__c obj2 = new Object2__c();
                      obj2.fld4 = question.response;
                      obj2.fld3 = question.someId;
                      obj2.Object__c = obj.Id;
                      surveyQuestionsRespList.add(obj2);
             }
             insert surveyQuestionsRespList;
    }
    global with sharing class Wrapper1
   {
          public String stringVar1{get;set;}
          public String stringVar2{get;set;}
          public List<Wrapper2> Wrapper2{get;set;}
    }
    global with sharing class Wrapper2
    {
         public String someId{get;set;}
         public String response{get;set;}
     }
}

Please help me how to generate JSON for this class. Thanks in Advance.

Regards
Venkat.
Best Answer chosen by Venkat
Amit Chaudhary 8Amit Chaudhary 8
Please try below JSON for your REST API wrapper
{
	"stringVar1":"demo",
	"stringVar2":"Test",
	"Wrapper2":[
				{ 	"someId":"SomeID",
				 	"response" :"SomeResponse"
				},
				{ 	"someId":"SomeID1",
				 	"response" :"SomeResponse1"
				}
		]
}
User-added image

Please check below post for JSON to APEX.
1) http://json2apexengine.somee.com/
2) https://www.adminbooster.com/tool/json2apex

Let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below JSON for your REST API wrapper
{
	"stringVar1":"demo",
	"stringVar2":"Test",
	"Wrapper2":[
				{ 	"someId":"SomeID",
				 	"response" :"SomeResponse"
				},
				{ 	"someId":"SomeID1",
				 	"response" :"SomeResponse1"
				}
		]
}
User-added image

Please check below post for JSON to APEX.
1) http://json2apexengine.somee.com/
2) https://www.adminbooster.com/tool/json2apex

Let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
VenkatVenkat
Hi Amit,

Thank You! It is working fine.

Thanks
Venkat.