0

am trying to get the value from the json file

Here is a live codepen of what I am working on codepen.io/krm218/pen/vGvaNN

Here is a screenshot of my json file or go to https://api.myjson.com/bins/4ky5s

JSON

I'm trying to retrieve the main-view, alt-view-1, etc.. from the images array. I've been able to get the other values like description and id with this code

 $.getJSON("json.json", function(data){
  $.each(data.products, function() {
  $("#results").append(
   this["id"] + this["description"]
   );   
  });  
});

When I do this["images"], I get

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

How can I get those values?

11
  • 1
    can you post the actual object as code? Commented May 6, 2016 at 21:50
  • 1
    images is another array, you can get those values the same way you're doing it with the other values, a loop and accessing the objects inside the array, and then the properties etc. Commented May 6, 2016 at 21:51
  • looks like you're coercing the image objects into strings. Are you using the + operator on this["images"] somewhere? Commented May 6, 2016 at 21:54
  • 1
    codepen.io/maio/pen/jqdPeB Commented May 6, 2016 at 22:25
  • 1
    @maioman that worked. Thank you!! It was this.images[0]['main-view'] Commented May 6, 2016 at 22:28

3 Answers 3

1

Here is how you can get in there:

Working Example

var arr = obj.products;

for (var i = 0; i < arr.length; i++) {
  var images = arr[i].images;
  for (var img in images) {
    console.log(images[img]);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Would this go inside the each loop that I have ? See code here codepen.io/krm218/pen/vGvaNN
well all you need to do now, is change var arr = obj.products to var arr = data.products, which I just tried in the pen and it works
I see! Thank you @JordanHendrix.
0

I modified your JSON a little, so that the images are stored as an object (like an associative array) under your product object, so here's where I started with the JSON (I only added one product):

var data = {
  products:[
    {
      description:'Matte black',
      id:1,
      images:{
        "main-view":'/images/1/mainView.png',
        "alt-view-1":"/images/1/altView1.png",
        "alt-view-2":"/images/1/altView2.png",
        "alt-view-3":"/images/1/altView3.png",
        "alt-view-4":"/images/1/altView4.png",
        "alt-view-5":"/images/1/altView5.png"
      }
    }
  ]
};

With them in that format, you can use the jQuery each function to loop through the images, with the parameters being the key name and value in the each function. You can build an image list of them (or use the values however) by looping and using k and v to print their key and value respectively:

$.each(data.products, function(){

  var imageList = $("<div>").addClass("image-list");

  $.each(this.images, function(k, v){
    imageList.append(
      $("<div>").addClass('image').append(
        $("<span>").addClass('image-name').append(k + " = "),
        $("<span>").addClass('image-url').append(v)
      )
    );
  });

  $('#results').append(
    $("<div>").addClass('product').append(
      $("<div>").addClass('product-id').append(this.id),
      $("<div>").addClass('product-desc').append(this.description),
      imageList
    )
  );
});

The results should appear like this:

1
Matte black
main-view = /images/1/mainView.png
alt-view-1 = /images/1/altView1.png
alt-view-2 = /images/1/altView2.png
alt-view-3 = /images/1/altView3.png
alt-view-4 = /images/1/altView4.png
alt-view-5 = /images/1/altView5.png

Here's a Fiddle demonstrating: https://jsfiddle.net/5yc733k4/

1 Comment

hi @mark.hch thanks for your response. Unfortunately I'm not able to change the json file structure so have to figure out how to do it the way it is.
0

this.images[0]['main-view'] did the trick!

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.