iterate through JSON of data
How This Works:
✅ Imports the object schema
@salesforce/schema/bscDealInfoData__c
@salesforce/schema/bscFormData__c
✅ Wires the field metadata
@wire(BSC_DEAL_INFO_FIELDS)
@wire(BSC_FORM_FIELDS)
✅ Creates a field label map dynamically
The mapFieldLabels(fieldData) function maps apiName → label.
✅ Replaces API names with labels in differences output Uses this.getFieldLabel(key) in compareVersions().
✅ Imports the object schema
@salesforce/schema/bscDealInfoData__c
@salesforce/schema/bscFormData__c
✅ Wires the field metadata
@wire(BSC_DEAL_INFO_FIELDS)
@wire(BSC_FORM_FIELDS)
✅ Creates a field label map dynamically
The mapFieldLabels(fieldData) function maps apiName → label.
✅ Replaces API names with labels in differences output Uses this.getFieldLabel(key) in compareVersions().
import { LightningElement } from 'lwc';
export default class CompareJsonData extends LightningElement {
    jsonData = {
        data: {
            bscDealInfoData: [
                {
                    Id: "3234",
                    version__c: 2,
                    Name: "bscDealinfo Name2",
                    eyey_test__c: "454398757439579834",
                    break__c: "345"
                },
                {
                    Id: "4884",
                    version__c: 1,
                    Name: "bscDealinfo Name2",
                    eyey_test__c: "0000000000000"
                }
            ],
            bscFormData: [
                {
                    Id: "50550",
                    createddate: "12-13-2025",
                    version__c: 2,
                    related_opp__c: "11111111111",
                    company__c: "2222222",
                    BSF_Name__c: "edited",
                    related_opp__r: {
                        Name: "Test Opp",
                        Id: "11111111111"
                    },
                    company__r: {
                        Name: "Company name",
                        Id: "2222222"
                    }
                },
                {
                    Id: "3884664",
                    createddate: "12-13-2025",
                    BSF_Name__c: "New",
                    version__c: 1,
                    related_opp__c: "11111111111",
                    company__c: "2222222",
                    related_opp__r: {
                        Name: "Test Opp",
                        Id: "11111111111"
                    },
                    company__r: {
                        Name: "Company name",
                        Id: "2222222"
                    }
                }
            ]
        }
    };
    differences = [];
    connectedCallback() {
        this.processData();
    }
    processData() {
        this.compareVersions(this.jsonData.data.bscDealInfoData, 'bscDealInfoData');
        this.compareVersions(this.jsonData.data.bscFormData, 'bscFormData');
        console.log("Differences:", this.differences);
    }
    compareVersions(dataList, dataType) {
        if (!Array.isArray(dataList) || dataList.length < 2) {
            return; // No comparison needed if only one version exists
        }
        // Sort data based on version__c (descending)
        dataList.sort((a, b) => b.version__c - a.version__c);
        const newData = dataList[0];
        const oldData = dataList[1];
        // Get all unique field keys from both old and new data
        const allKeys = new Set([...Object.keys(newData), ...Object.keys(oldData)]);
        for (const key of allKeys) {
            if (this.isIgnoredField(key)) {
                continue; // Skip ignored fields
            }
            let oldValue = oldData[key] !== undefined ? oldData[key] : null;
            let newValue = newData[key] !== undefined ? newData[key] : null;
            // If field is in related fields, replace with name from __r
            if (this.isRelatedField(key)) {
                oldValue = oldData[`${key.replace("__c", "__r")}`]?.Name || oldValue;
                newValue = newData[`${key.replace("__c", "__r")}`]?.Name || newValue;
            }
            // Push to differences if changed or newly added
            if (oldValue !== newValue) {
                this.differences.push({
                    fieldApiName: key,
                    dataset: dataType, // Indicate which dataset this key belongs to
                    oldvalue: oldValue,
                    newvalue: newValue
                });
            }
        }
    }
    isIgnoredField(key) {
        return ["Id", "createddate", "version__c"].includes(key);
    }
    isRelatedField(key) {
        return ["company__c", "related_opp__c"].includes(key);
    }
    get formattedOutput() {
        return `${JSON.stringify({ differences: this.differences }, null, 4)}
`;
    }
}
iterate through JSON of data
![iterate through JSON of data]() Reviewed by dasfrogpractice
        on 
        
05:09
 
        Rating:
 
        Reviewed by dasfrogpractice
        on 
        
05:09
 
        Rating: 
       
 
 
 
 
 
No comments: