1

I have an object called AssessmentForm which has a lot of fields. Through an existing APEX controller I can access these specific fields by AssessmentForm.fieldname.

Is there a way to convert all of the fields into a JSON?

For example:

Object: AssessmentForm

Which has the following fields: field1 = 5, field2 = 'cat', field3 = 54, field4 = 'dog

Would be converted to: {"field1": 5, "field2" : "cat", "field3" : 54, "field4": "dog"}

For extra context, what I would like to do is send all of the form data through an API. To do so, I want to convert all of the fields in AssessmentForm into a JSON. The form itself has a lot of fields on it and so I'm hoping there might be a way to convert an object's fields to a JSON without having to manually code it all out.

1 Answer 1

2

Yes, you can do it in one line:

String assessmentFormString = JSON.serialize(form);

This assumes your form is the variable name of your apex class instance.

This JSON output can be directly sent to your API in the body of your request.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .