You can do this very easily using <apex:remoteObjects>
.
First you will want to define the fields on the object you care about. Here's how you do it for the Contact object.
<apex:remoteObjects >
<apex:remoteObjectModel name="Contact" jsShorthand="cont" fields="Name,Id,FirstName,LastName,Phone,Email,Title,AccountId">
</apex:remoteObjectModel>
</apex:remoteObjects>
This can appear anywhere within your <apex:page>
tag.
Next you'll want to write the Javascript to parse the JSON and insert the new record.
//Create the JS object from the JSON
var contactFromJSON = JSON.parse('{!YOUR_JSON_HERE}');
//Function to Create Contact record
function Createcontact(contact){
var createCon = new SObjectModel.cont({
FirstName: contact.FirstName,
LastName: contact.LastName,
Title: contact.Title,
Phone: contact.Phone,
Email: contact.Email});
createCon.create();
}
Then the code to insert the new Contact will just pass the Object which is taken from the JSON as an argument to the function like this:
createContact(contactFromJSON);
Now you've created a new Contact. Hope that answers your question. For more information about remoteObjects
, see Building Single Page Apps with JavaScript and Visualforce Remote Objects.