0

I'm trying to get data from a JSON array within a JSON array, but am having little success.

So our server returns a JSON array within a variable called 'msg', which look like this when I console.log it

[{"product":"[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2",\"productCode\":\"332388\"}]","category":"gross_MAX_1","totalCategory":1},{"product":"[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2",\"productCode\":\"332388\"}]","category":"gross_MAX_1","totalCategory":1}]

I then have the following code to pull out the product name but keep getting an undefined error - Uncaught TypeError: Cannot read property 'prodName' of undefined

console.log(msg);
var data = $.parseJSON(msg);
console.log(data);

OBJ = {};
OBJ.apply = function(data) {
    $.each(data, function() {
        $.each(this, function(key, value) {
            console.log(value.product.prodName);
        });
    });
};
OBJ.apply(data);

Any ideas where i'm going wrong?

Thanks

EDIT

I've looked at the parsed variable and I get:-

[Object, Object]
0: Object
category: "restrictedItems_ie-standard-gross_MAX_1"
product: "[{"imgUrl":"/p/332401","prodCartQty":2,"prodName":"Test Product 1","productCode":"332401"},{"imgUrl":"/p/332388","prodCartQty":2,"prodName":"Test Product 2","productCode":"332388"}]"
totalCategory: 1
__proto__: Object
1: Object
category: "restrictedItems_ie-standard-gross_MAX_1"
product: "[{"imgUrl":"/p/332401","prodCartQty":2,"prodName":"Test Product 1","productCode":"332401"},{"imgUrl":"/p/332388","prodCartQty":2,"prodName":"Test Product 2","productCode":"332388"}]"
totalCategory: 1
__proto__: Object
length: 2
__proto__: Array[0]

So the data parses fine but now I need to return the data in product or get the object data in Product into a separate object. Any ideas? thanks

David
  • 25
  • 6
  • Your $.each(this, function(key, value) is pretty useless, if you just want to shop up thfe productname – xNeyte Apr 29 '15 at 12:43
  • Could you not change the format of the response at the server? It seems a little odd having JSON data within JSON data. – Rory McCrossan Apr 29 '15 at 12:45
  • from which server side application you're getting `json` i means you would be getting json from controller action or from service(API)? – Kaushik Apr 29 '15 at 13:42

2 Answers2

0

Looks like it needs to be unescaped.

If you paste the JSON in a tool like this JSON formatter, you'll be able to see more clearly how it doesn't like the \s.

Community
  • 1
  • 1
Pudd
  • 449
  • 3
  • 13
0

Try this. I had to fix the invalid JSON by escaping the trailing quote on each

\"Test Product 1" <<<<
\"Test Product 2" <<<<

var data = [
    {
        "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2\",\"productCode\":\"332388\"}]",
        "category": "gross_MAX_1",
        "totalCategory": 1
    },
    {
        "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2\",\"productCode\":\"332388\"}]",
        "category": "gross_MAX_1",
        "totalCategory": 1
    }
]



$.each(data,function(_,productArr) {
  var arr = JSON.parse(productArr.product.replace(/\//g,""));
  $.each(arr,function(_,productDetails) {
    // here is a product detail object
    $.each(productDetails,function(name,value) {
      console.log(name,value);
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Hi, the json the server return has another json array inside it, I've been told this is the way the server has to return it. How can I work with what the server return? I need to get the info from the product section of the object. – David Apr 29 '15 at 12:59
  • Is there a way to get just the product array out of the object? – David Apr 29 '15 at 13:00
  • Hi, that did the trick thanks. I've now got the data in a format I can use. Top man. – David May 05 '15 at 08:51