How to Send and Receive JSON Data Using Salesforce REST API | Apex – Posting a JSON to REST resource in Salesforce | Using REST(JSON) Get data from External website

Image 529 views

Salesforce REST API communicates using JSON (JavaScript Object Notation), making it simple and lightweight for data exchange between Salesforce and external applications. JSON is easy to read, platform-independent, and widely used in modern integrations.

In this article, you’ll learn how to:

  • Send JSON data to Salesforce (create records).
  • Receive JSON responses from Salesforce (query records)
  • Use simple HTML + JavaScript to test API calls

📤 Send JSON Data to Salesforce

When creating a record in Salesforce, you send JSON in the request body:

👉 Download Free Ebook →

👉 Get Free Course →

Image
  1.    {
  2.   "Name": "API Created Account",
  3.   "Phone": "123-456-7890"
  4.  }

💻 HTML + JavaScript Example (Send & Receive JSON)

  1.   <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>Salesforce JSON API Demo</title>
  5. </head>
  6. <body>
  7.   <h2>Send & Receive JSON WITH Salesforce</h2>
  8.   <button onclick="createAccount()">CREATE Account</button>
  9.   <button onclick="getAccounts()">GET Accounts</button>
  10.   <div id="output"></div>
  11.  
  12.   <script>
  13.     const accessToken = "YOUR_ACCESS_TOKEN";
  14.     const instanceUrl = "https://yourInstance.salesforce.com";
  15.  
  16.     async FUNCTION createAccount() {
  17.       const response = await fetch(`${instanceUrl}/services/data/v59.0/sobjects/Account`, {
  18.         method: "POST",
  19.         headers: {
  20.           "Authorization": `Bearer ${accessToken}`,
  21.           "Content-Type": "application/json"
  22.         },
  23.         body: JSON.stringify({ Name: "API Created Account", Phone: "123-456-7890" })
  24.       });
  25.       const RESULT = await response.json();
  26.       document.getElementById("output").textContent = JSON.stringify(RESULT, NULL, 2);
  27.     }
  28.  
  29.     async FUNCTION getAccounts() {
  30.       const response = await fetch(`${instanceUrl}/services/data/v59.0/query?q=SELECT+Id,Name,Phone+FROM+Account+LIMIT+5`, {
  31.         method: "GET",
  32.         headers: {
  33.           "Authorization": `Bearer ${accessToken}`
  34.         }
  35.       });
  36.       const RESULT = await response.json();
  37.       document.getElementById("output").textContent = JSON.stringify(RESULT, NULL, 2);
  38.     }
  39.   </script>
  40. </body>
  41. </html>

 

With Salesforce REST API, sending and receiving JSON data is simple and efficient. You can create records by sending JSON payloads and retrieve data in JSON format for easy processing in your applications.

In the next article, we’ll cover using Named Credentials in Salesforce for secure REST API calls.

 

FAQ (Frequently Asked Questions)

What is a send HTTP request?

Send HTTP Request is an asynchronous activity that sends an HTTP request and waits for a response from the web server. This activity sends a request to a server that is compliant with either the HTTP 1.0, 1.1, or 2.0 specification.

How to send an HTTP request to API?

After you specify the request protocol, method, and URL, add any other details required by the API you're sending the request to: Specify any parameters and body data or request headers you need to send with the request. Set up any required authentication and authorization.

What is a send request?

The Send Request operation sends an HTTP request to a specified URL using a specified HTTP request method.

 
  
👉 Get Free Course → Image

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

Image

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Image

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net