0

I have a JSON like this:

    var parse = [
   {
      "variation_id":"34",
      "attributes":{
         "attribute_pa_rozmiar":"m"
      },
      "image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish-300x300.jpg",
      "image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish.jpg",
      "image_title":"Jellyfish",
      "price_html":"",
      "availability_html":"<p class=\"stock \">10 w magazynie</p>",
      "sku":"",
      "weight":"4 kg",
      "dimensions":"",
      "min_qty":1,
      "max_qty":"10",
      "backorders_allowed":false,
      "is_in_stock":true,
      "is_downloadable":false,
      "is_virtual":false,
      "is_sold_individually":"no"
   },
   {
      "variation_id":"33",
      "attributes":{
         "attribute_pa_rozmiar":"s"
      },
      "image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert-300x300.jpg",
      "image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert.jpg",
      "image_title":"Desert",
      "price_html":"",
      "availability_html":"<p class=\"stock \">5 w magazynie</p>",
      "sku":"",
      "weight":"4 kg",
      "dimensions":"",
      "min_qty":1,
      "max_qty":"5",
      "backorders_allowed":false,
      "is_in_stock":true,
      "is_downloadable":false,
      "is_virtual":false,
      "is_sold_individually":"no"
   }
][
   {
      "variation_id":"34",
      "attributes":{
         "attribute_pa_rozmiar":"m"
      },
      "image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish-300x300.jpg",
      "image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish.jpg",
      "image_title":"Jellyfish",
      "price_html":"",
      "availability_html":"<p class=\"stock \">10 w magazynie</p>",
      "sku":"",
      "weight":"4 kg",
      "dimensions":"",
      "min_qty":1,
      "max_qty":"10",
      "backorders_allowed":false,
      "is_in_stock":true,
      "is_downloadable":false,
      "is_virtual":false,
      "is_sold_individually":"no"
   },
   {
      "variation_id":"33",
      "attributes":{
         "attribute_pa_rozmiar":"s"
      },
      "image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert-300x300.jpg",
      "image_link":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Desert.jpg",
      "image_title":"Desert",
      "price_html":"",
      "availability_html":"<p class=\"stock \">5 w magazynie</p>",
      "sku":"",
      "weight":"4 kg",
      "dimensions":"",
      "min_qty":1,
      "max_qty":"5",
      "backorders_allowed":false,
      "is_in_stock":true,
      "is_downloadable":false,
      "is_virtual":false,
      "is_sold_individually":"no"
   }
]

What I need is to run a search inside attributes, find if this Object has, for eg. attribute_pa_rozmiar=m and get corresponding image_src. Sorry for such a bloated JSON, but it's the only way I could get it out.

vendettamit
  • 14,315
  • 2
  • 32
  • 54
Tomek Buszewski
  • 7,659
  • 14
  • 67
  • 112
  • Can you show us how you tried? – casraf Jul 01 '13 at 12:46
  • My JSON is something like this: `var parse = [{"attributes":{"attribute_s":"s"},...]`. So I went with `$.each(parse.attributes, function(i,v) { if (v.attributes.search(new RegExp(/s/i)) != -1) {alert(v.attributes}); but it returns "Uncaught TypeError: Cannot read property 'length' of undefined" in the console. – Tomek Buszewski Jul 01 '13 at 12:50
  • @TomekBuszewski: You want `$.[…](parse, …` then - `parse` has no `attributes` property but is an array – Bergi Jul 01 '13 at 12:54

2 Answers2

0

I tried running $.each and $.map functions based on found exaples, but none worked.

Try $.grep instead:

var filtered = $.grep(parse, function(item) {
    return item.attributes.attribute_pa_rozmiar == "m";
});
var images = $.map(filtered, function(item) {
    return item.image_src;
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Returns `Uncaught TypeError: Cannot read property 'length' of undefined`; – Tomek Buszewski Jul 01 '13 at 12:55
  • Not for the object you presented. For the one you posted in the comment it would be `$.grep(parse, function(v) { return v.attributes.attribute_s.indexOf("s") != -1; })` – Bergi Jul 01 '13 at 12:58
0

Try this.. it's using the $.each and working fine

      $.each(parse, function(i, item) {
          $.each(item, function(j, innerItem){
           // perform search on innerItem.a or innerItem.b
          });
      });

Updated:

As per your new update first your JSON was invalid. You have extra ][ in the middle of the variable data. I've create another JSFiddle to solve your problem. See the working demo:

http://jsfiddle.net/vendettamit/kjzPC/

PS - When you have to post this kinda long codes then you can use online formatter and then post it. :)

vendettamit
  • 14,315
  • 2
  • 32
  • 54