0

I have a simple json in which I want to manipulate array structure and data as below , Can anyone suggest How can I do this using javascript or xpaths.

current json :-

{
  "pimage": [
    {
      "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_sb.jpg",
      "timage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_s.gif",
      "mimage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011fp.gif",
      "lfimage": "http://img.bbystatic.com/BestBuy_US/images/products/7306/7306011_sa.jpg"
    }
  ]
}

expected :- below limage key is made common and urls values from current json is kept as it is.

{
  "pimage": [
    {
      "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_sb.jpg"
    },
    {
      "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_s.gif"
    },
    {
      "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_s.gif"
    },
    {
      "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_s.gif"
    }
  ]
}
3
  • @NinaScholz rule ? i dint get you , can you please elaborate Commented Feb 9, 2016 at 9:51
  • how is the current to the expected mapped. a rule is not visible. Commented Feb 9, 2016 at 9:52
  • what happen to '.../7306011_sa.jpg' string? Commented Feb 9, 2016 at 9:56

3 Answers 3

1

Question feels a bit unclear, but this code will turn your object into the form you wanted:

var myObj = {
  "pimage": [
    {
      "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_sb.jpg",
      "timage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_s.gif",
      "mimage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011fp.gif",
      "lfimage": "http://img.bbystatic.com/BestBuy_US/images/products/7306/7306011_sa.jpg"
    }
  ]
}
var myFunc = function(obj) { 
  var keys = Object.keys(obj.pimage[0]);
  var newObj = {"pimage":[]};
  keys.forEach(function(k) {
    var newImg = {};
    newImg['limage'] = obj.pimage[0][k];
    newObj.pimage.push(newImg);
  });
  return newObj;
}

console.log(myFunc(myObj));

With more information, it the myFunc() function could be crafted a bit more robustly.

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

1 Comment

@milerbr , I used push with key value and it worked. thanks
0

This solution maps every key/value pair to a seperated object.

var obj = {
        "pimage": [{
            "limage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_sb.jpg",
            "timage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011_s.gif",
            "mimage": "http://images.bestbuy.com/BestBuy_US/images/products/7306/7306011fp.gif",
            "lfimage": "http://img.bbystatic.com/BestBuy_US/images/products/7306/7306011_sa.jpg"
        }]
    };

obj.pimage = Object.keys(obj.pimage[0]).map(function (k) {
    var o = {};
    o[k] = obj.pimage[0][k];
    return o;
});

document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');

Comments

0

Try this :

function Parse(ur_obj) {
   var iterate = ur_obj.pimage[0];
   var result = [];
   for(var x in iterate) {
      result.push({x:iterate[x]});
   }
   ur_obj.pimage = result;
   return ur_obj;
}

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.