How to remove attributes tag from JSON in JavaScript

Side Hustle Freelancer

How to remove attributes tag from JSON in JavaScript

Today we talk about how to remove a grafted attribute in an object in JavaScript. The script that I propose also works for a more complex tree, with various objects nested.

Example of the structure:

Use case

The main purpose of this post is to share this knowledge that I did not find easily on Google.
The underlying problem was the removal of an “attributes” tag that is automatically generated when a custom JSON is exported from a lightning page – Salesforce.
For example:

How it works

If you’re unfamiliar with JavaScript, but you know Salesforce, imagine attributes as if it were an object field.

The algorithm itself is not difficult, but we must pay attention to the flexibility of Javascript. In fact, an attributes can take the value of Array, Object/Map or “field/var”.

The logic of the script is to:

  1. “Cycle” all the attributes of an object, for each one check if it is an array or an object;
  2. If it is an array, then cycle all the elements of the array and redo step 1;
  3. If it is an object check if the attribute to be deleted is part of its attributes (hasOwnProperty () method), in that case delete it and then start again from step 1;
  4. If the attribute is a standard field then continue;

The instanceof method helps us to understand if we are dealing with an Array or an Object:

if(obj instanceof Array) {
  return true;

To remove the attribute we use the method: delete obj.attributes;


Here is the code that removes the attributes automatically, in an object or a chain of nested objects / arrays

//JavaScript Lightning (Salesforce)
removeAttributes : function(obj){
   if(!obj instanceof Array && !obj instanceof Object) {
   for(var i in obj) {
       if(obj instanceof Array || (obj instanceof Object && obj.hasOwnProperty(i))) {
           if(i == 'attributes') {
               delete obj.attributes;


Nessun commento

Aggiungi il tuo commento