2

I want to delete __metadata where ever it appears .How to do that I searched this question but didn't get exact solution. hope you will help me out .

{
  "__metadata":{
     "uri":"asd",
     "type":"DP_CART_SRV.DPCARTHeader"
  },
  "Dealer":{
     "__metadata":{
        "uri":"asd"
     },
     "CustomerNo":"",
     "Name1":"",
     "Name2":""
  },
  "Retailer":{
     "__metadata":{
        "uri":"asd"
     },
     "CustomerNo":"",
     "Name1":"",
     "Name2":""
  },
  "Cart":"0000000081",
  "Type":"SH",
  "CreatedOn":"/Date(1399420800000)/",
  "ChangedOn":null,
  "OrderId":"",
  "OrderType":"",
  "ReqDeliveryDate":"2014/05/31",
  "OrderValue":"11.00",
  "DocCurrency":"EUR",
  "NetValue":"11.00",
  "Freight":"0.00",
  "Discount":"0.00",
  "Tax":"0.00",
  "Remarks":"Remarks",
  "Items":{
     "results":[
        {
           "__metadata":{
              "uri":"asd"
           },
           "Cart":"0000000081",
           "ItemNo":"000010",
           "ProductID":"FAN_FG1",
           "ItemDesc":"Finshed product FAN",
           "Quantity":"1.000",
           "Uom":"KAR",
           "Price":"11.00",
           "Currency":"EUR",
           "Available":"",
           "DeleteStatus":""
        },
        {
           "__metadata":{
              "uri":"",
              "type":""
           },
           "DeleteStatus":"",
           "Available":"",
           "Currency":"",
           "Price":"",
           "Uom":"",
           "Quantity":"",
           "ItemDesc":"",
           "ProductID":"",
           "ItemNo":"",
           "Cart":""
        }
     ]
  }
}

2 Answers 2

3

Here is a fun one. Not very obvious way how you can do it:

var result = JSON.parse(JSON.stringify(obj, function(key, value) {
    return key !== '__metadata' ? value : undefined;
}));

It makes use of the replacer function JSON.stringify method accepts:

replacer If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the final string. A detailed description of the replacer function is provided in the javaScript guide article Using native JSON.

However be aware that this is not intended JSON.stringify and JSON.parse usage. Also of course the result will no longer be the original object, but a new object.

Demo: http://jsfiddle.net/7Gj47/

Sign up to request clarification or add additional context in comments.

Comments

3

I don't really know what you mean by "nested json attribute". The OP appears to be an object literal, which is the syntax used for JSON, however JSON is a string.

Anyhow, supposing you have JSON, it can be turned into an object using JSON.parse. You can then iterate over the object and remove any property with a certain name, and recursively remove properties from "nested" objects using a function like:

function removeProp(obj, propName) {

  for (var p in obj) {

    if (obj.hasOwnProperty(p)) {

      if (p == propName) {
        delete obj[p];

      } else if (typeof obj[p] == 'object') {
        removeProp(obj[p], propName);
      }
    }
  }
  return obj;
}

So if you have JSON (i.e. text representing an object), you can remove all instances of a particular property using something like:

var jsonText = '{ ... }';

var obj = JSON.parse(jsonText);
removeProp(obj, '__metadata');
jsonText = JSON.stringify(obj);

The function could take a third parameter that specifies if it should check nested objects or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.