Skip to main content

Latest Post

How to Set Up Two-Factor Time-Based One-Time Password (TOTP) Authentication on iPhone Without Third-Party Apps

Unlocking an additional layer of safety to your iPhone is less difficult than you might suppose. With Two-Factor Time-Based One-Time Password (TOTP) authentication, you may bolster your device's protection and other website safety without relying on 1/3-party apps. Here's how you could set it up:

Demystifying Salesforce's json.deserializeUntyped Method - Full Code with Output

Introduction

In Salesforce, working with JSON data is a common requirement when dealing with integrations, web services, or handling data from external sources. Salesforce provides several methods to parse JSON data into native Apex objects. One such method is json.deserializeUntyped, which allows developers to deserialize JSON data into an untyped Object. This gives you the flexibility to work with JSON data without defining specific classes.


In this blog, we'll explore the json.deserializeUntyped method in Salesforce, understand its usage, and provide a step-by-step example with full code and output.


What is json.deserializeUntyped?


The json.deseriadlizeUntyped method is part of Salesforce's Apex language, specifically designed to deserialize JSON data into an Object. This means that the result of the deserialization will be a generic Object instance, and you'll need to cast it to a specific type if you want to access its properties.


Example Scenario


Let's assume we have a JSON response from an external API that we want to parse and extract information from. The JSON response looks like this:


{

  "name": "John Doe",

  "age": 30,

  "email": "john.doe@example.com",

  "isSubscribed": true,

  "preferences": {

    "theme": "light",

    "language": "en"

  }

}


Our goal is to deserialize this JSON data into an untyped object and then access its properties to display the information.


Step-by-Step Example

  1. Create a new Apex class to house the code. Let's call it JsonParsingExample.
  2. Define the method to perform the deserialization and access the properties:

public class JsonParsingExample {


    public static void parseJsonData(String jsonString) {

        // Deserialize the JSON data into an untyped object

        Object untypedObj = (Object) JSON.deserializeUntyped(jsonString);


        // Access the properties of the untyped object

        String name = (String) untypedObj.get('name');

        Integer age = (Integer) untypedObj.get('age');

        String email = (String) untypedObj.get('email');

        Boolean isSubscribed = (Boolean) untypedObj.get('isSubscribed');


        Map<String, Object> preferencesMap = (Map<String, Object>) untypedObj.get('preferences');

        String theme = (String) preferencesMap.get('theme');

        String language = (String) preferencesMap.get('language');


        // Display the extracted information

        System.debug('Name: ' + name);

        System.debug('Age: ' + age);

        System.debug('Email: ' + email);

        System.debug('Is Subscribed: ' + isSubscribed);

        System.debug('Theme: ' + theme);

        System.debug('Language: ' + language);

    }

}


3. Now, call the parseJsonData method with the JSON data we have:


public class JsonParsingExampleDemo {

    public static void main(String[] args) {

        String jsonString = '{ "name": "John Doe", "age": 30, "email": "john.doe@example.com", "isSubscribed": true, "preferences": { "theme": "light", "language": "en" } }';

        

        // Call the parseJsonData method to perform deserialization and display the output

        JsonParsingExample.parseJsonData(jsonString);

    }

}


Output


When you execute the JsonParsingExampleDemo class, the debug logs will show the extracted information from the JSON:


DEBUG|Name: John Doe

DEBUG|Age: 30

DEBUG|Email: john.doe@example.com

DEBUG|Is Subscribed: true

DEBUG|Theme: light

DEBUG|Language: en



Conclusion


In this blog post, we explored the json.deserializeUntyped method in Salesforce, which allows us to parse JSON data into an untyped Object. We walked through a step-by-step example of how to use this method to extract information from a JSON response and display it in the debug logs. This method provides flexibility when working with JSON data in Salesforce, especially when you don't want to create specific Apex classes for deserialization. Remember to cast the untyped object appropriately to access its properties.


I hope you found this blog helpful in understanding the json.deserializeUntyped method in Salesforce. Happy coding!

Popular Posts

Uninstall all Windows 10 default apps using Powershell

Here is script to uninstall all windows 10 default modern apps. This script uninstalls xbox, xbox Game bar, Xbox App,Xbox Gaming Overlay, Get started etc from your computer. No need to run one by one commands Just copy below script, run  powershell as administrator and paste script and press enter . It will automatically uninstall all default programs.  If you do not  want to uninstall some apps than just remove " "  line from script. $packages = @( "7EE7776C.LinkedInforWindows" "C27EB4BA.DropboxOEM" "Microsoft.3DBuilder" "Microsoft.Microsoft3DViewer"

Drag and drop, show and hide columns styling with SLDS Customize list view Visualforce Page and JQuery

Here is sample code for who wants drag and drop, Show and hide functionality in visualforce page using SLDS styling. In this code we are using JQuery, SLDS, Visualforce page. Customize List View  Sample Visualforce Page:-  <apex:page showHeader="false" doctype="html-5.0"  sidebar="false" lightningStylesheets="true">

SalesForce.com Icons Available for Use at one place

A picture is worth a thousand words  it is also applies on salesforce to  visualize data. Salesforce provides various standard icons which is used in their own Data.  you can put image based on your requirement and condition of data. for example : progress bar on field in salesforce and due date over message. Read more...

LWC Full Dynamic Working Code for LWC Datatable CSS Styling

Introduction: In this blog post, we will explore how to apply dynamic CSS styling to a Lightning Web Component (LWC) Datatable. LWC is a powerful framework provided by Salesforce for building web components on the Lightning Platform. The Datatable component allows us to display tabular data in a structured and organized manner. By leveraging its features and using CSS styling, we can enhance the visual appearance and user experience of our LWC applications. Let's dive into the details and learn how to implement dynamic CSS styling for the LWC Datatable.

Dynamic Conditional Rendering in LWC: Implementing IF:TRUE

Introduction: In Lightning Web Components (LWC), conditional rendering allows us to selectively display or hide elements based on certain conditions. One common scenario is rendering content when a condition evaluates to true. In this blog post, we will explore how to implement dynamic conditional rendering using the IF:TRUE directive in LWC. We will walk through an example to demonstrate a full working code that achieves this functionality. Let's get started!

Examples of code for implementing pagination with LWC

A Lightning Web Component (LWC) pagination code that you can use to implement pagination in your LWC components. HTML Markup (pagination.html): <template>   <div class="container">     <template if:true={displayItems}>       <ul>         <template for:each={displayItems} for:item="item">           <li key={item.id}>{item.name}</li>         </template>       </ul>     </template>     <div class="pagination">       <lightning-button-icon         icon-name="utility:chevronleft"         disabled={currentPage === 1}         onclick={previousPage}         alternative-text="Previous"         title="Previous"       ></lightning-button-icon> ...

A Simple Way to Understand Salesforce

Introduction: Salesforce is a powerful cloud-based customer relationship management (CRM) platform that has revolutionized the way businesses manage their sales, customer service, marketing, and more. However, understanding Salesforce can sometimes be a daunting task for newcomers. In this blog post, we will break down Salesforce into simple terms and provide you with a basic understanding of its key components and functionalities.