Mayur Tripathi
Mayur Tripathi

Reputation: 71

How to remove the element from the json structure without its values by javascript?

This is the structure that i am having, i converted from the xml to json, but now i dont need the root element and also the element tag:

{
"root": {
    "batchRequests": [
        {
            "element": {
                "method": "PATCH",
                "url": "url",
                "richInput": {
                    "Error_Message__c": {},
                    "Status__c": "0"
                }
            }
        },
        {
            "element": {
                "method": "PATCH",
                "url": "url",
                "richInput": {
                    "Error_Message__c": {},
                    "Status__c": "0"
                }
            }
        }
    ]
}

And I want to transform that into this structure:

{
"batchRequests": [
    {
        "method": "PATCH",
        "url": "url",
        "richInput": {
            "Error_Message__c": "",
            "Status__c": "0"
        }
    },
    {
        "method": "PATCH",
        "url": "url",
        "richInput": {
            "Error_Message__c": "",
            "Status__c": "1"
        }
    }
]
}

Can you please help me with this transformation, if it is possible from javascript?

Upvotes: 1

Views: 58

Answers (3)

abd995
abd995

Reputation: 1839

You can do it with this single line.

 yourObject = { batchRequests: yourObject.root.batchRequests.map((x) => x.element) }

Try it here.

var yourObject = {
  "root": {
    "batchRequests": [{
        "element": {
          "method": "PATCH",
          "url": "url",
          "richInput": {
            "Error_Message__c": {},
            "Status__c": "0"
          }
        }
      },
      {
        "element": {
          "method": "PATCH",
          "url": "url",
          "richInput": {
            "Error_Message__c": {},
            "Status__c": "0"
          }
        }
      }
    ]
  }
}

yourObject = { batchRequests: yourObject.root.batchRequests.map((x) => x.element) }

console.log(yourObject);

Upvotes: 2

Nico Diz
Nico Diz

Reputation: 1504

  1. Parse batchRequests with map function:

    const batchRequests = data1.root.batchRequests.map(el => el.element)

  2. Create new object with this data:

    const data2 = { batchRequests }

const data1 = {
  "root": {
    "batchRequests": [
        {
            "element": {
                "method": "PATCH",
                "url": "url",
                "richInput": {
                    "Error_Message__c": {},
                    "Status__c": "0"
                }
            }
        },
        {
            "element": {
                "method": "PATCH",
                "url": "url",
                "richInput": {
                    "Error_Message__c": {},
                    "Status__c": "0"
                }
            }
        }
    ]
  }
}

const batchRequests = data1.root.batchRequests.map(el => el.element)

const data2 = { batchRequests }
console.log(data2)

Upvotes: 0

jimboweb
jimboweb

Reputation: 4542

If your json item is called json:

let result = {batchRequests:[]};
for(let item of json.root.batchRequests){
    result.batchRequests.push(item.element);
}

Upvotes: 0

Related Questions