javascript - Removing top level elements from a JSON - Stack Overflow

I have following input json payload,{"Products": {"Product": [{"ProductID"

I have following input json payload,

{
    "Products": {
        "Product": [
            {
                "ProductID": 458761,
                "Designation": "CB 024-2001",
                "EntryDate": "2002-01-20T19:00:00.000-05:00",
                "S1": "024",
                "S2": 2001,
                "Year": 2001
            },
            {
                "ProductID": 458234,
                "Designation": "AGRS03/08",
                "EntryDate": "2008-03-05T19:00:00.000-05:00",
                "S1": "03",
                "S2": "08",
                "Year": 2008
            }
        ]
    }
}

And now I need to transform it into the following JSON format.

[
    {
        "Designation": "CB 024-2001",
        "EntryDate": "2002-01-20T19:00:00.000-05:00",
        "ProductID": 458761,
        "S1": "024",
        "S2": 2001,
        "Year": 2001
    },
    {
        "Designation": "AGRS03/08",
        "EntryDate": "2008-03-05T19:00:00.000-05:00",
        "ProductID": 458761,
        "S1": "03",
        "S2": "08",
        "Year": 2008
    }
]

Can someone please help me to write a JavaScript to achieve this task. Any help is really appreciated.

I have following input json payload,

{
    "Products": {
        "Product": [
            {
                "ProductID": 458761,
                "Designation": "CB 024-2001",
                "EntryDate": "2002-01-20T19:00:00.000-05:00",
                "S1": "024",
                "S2": 2001,
                "Year": 2001
            },
            {
                "ProductID": 458234,
                "Designation": "AGRS03/08",
                "EntryDate": "2008-03-05T19:00:00.000-05:00",
                "S1": "03",
                "S2": "08",
                "Year": 2008
            }
        ]
    }
}

And now I need to transform it into the following JSON format.

[
    {
        "Designation": "CB 024-2001",
        "EntryDate": "2002-01-20T19:00:00.000-05:00",
        "ProductID": 458761,
        "S1": "024",
        "S2": 2001,
        "Year": 2001
    },
    {
        "Designation": "AGRS03/08",
        "EntryDate": "2008-03-05T19:00:00.000-05:00",
        "ProductID": 458761,
        "S1": "03",
        "S2": "08",
        "Year": 2008
    }
]

Can someone please help me to write a JavaScript to achieve this task. Any help is really appreciated.

Share Improve this question edited Mar 29, 2016 at 22:52 Ravindra Ranwala asked Mar 29, 2016 at 22:38 Ravindra RanwalaRavindra Ranwala 21.1k7 gold badges48 silver badges65 bronze badges 2
  • 1 I don't get it. It already is in that format. Once parsed, all you need to do is select the nested Array. I mean you can run through and delete the ProductID if you wish, but that's trivial. – user1106925 Commented Mar 29, 2016 at 22:47
  • 2 So you do want ProductID in there as well? Then just access the array directly. obj.Products.Product. Bam, easy as that. – Mike Cluck Commented Mar 29, 2016 at 22:52
Add a ment  | 

4 Answers 4

Reset to default 3

EDIT: You changed the question :(

Assuming your original json is stored in a variable named input. This can be done using this code:

var output = input.Products.Product;

ORIGINAL: You can do this using map:

var output = input.Products.Product.map(function(inObj) {
    return {
        "Designation": inObj.Designation,
        "EntryDate": inObj.EntryDate,
        "S1": inObj.S1,
        "S2": inObj.S2,
        "Year": inObj.Year
    }
});

This will give you the output you want - an array of objects, with the ProductIDs removed. I'm a bit rusty when it es to working with object references, but you could possibly shorten this using delete:

var output = input.Products.Product.map(function(inObj) {
    var outObj =  inObj;
    delete outObj.ProductID;
    return outObj;
});

This will change the original input values as well though, so i wouldn't remend it unless you don't plan on using that data again.

var first = {
  "Products": {
      "Product": [
          {
              "ProductID": 458761,
              "Designation": "CB 024-2001",
              "EntryDate": "2002-01-20T19:00:00.000-05:00",
              "S1": "024",
              "S2": 2001,
              "Year": 2001
          },
          {
              "ProductID": 458234,
              "Designation": "AGRS03/08",
              "EntryDate": "2008-03-05T19:00:00.000-05:00",
              "S1": "03",
              "S2": "08",
              "Year": 2008
          }
      ]
  }
}

then:

var second = first.Products.Product;

To make it exactly like you want:

for(var i = 0; i<second.length; i++){

 delete second[i].ProductID;

 }

You can use this little function:

var a = {
  "Products": {
    "Product": [{
      "ProductID": 458761,
      "Designation": "CB 024-2001",
      "EntryDate": "2002-01-20T19:00:00.000-05:00",
      "S1": "024",
      "S2": 2001,
      "Year": 2001
    }, {
      "ProductID": 458234,
      "Designation": "AGRS03/08",
      "EntryDate": "2008-03-05T19:00:00.000-05:00",
      "S1": "03",
      "S2": "08",
      "Year": 2008
    }]
  }
};


function newJSON(array){
  var b = array.Products.Product;
  b.forEach(function(e){delete e.ProductID});
  return JSON.stringify(b);
}

document.write(newJSON(a));

With underscore:

var result = _.map(data.Products.Products, (product) => {
    return _.omit(product, 'ProductID');
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745384661a4625384.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信