3

I'm trying to query a JSON file in my own server using $.getJSON and then cycling inside the objects. No problem so far, then i have an ID which is the name of the object i want to return, but can't seem to get it right:

  var id = 301;
  var url = "path/to/file.json";
  $.getJSON( url, function( json ) {
    var items = [];
    items = json;
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      console.log(item);
    }
  });

This prints the following in the console:

console.log of all the objects in json

Now let's say i want return only the object == to id so then i can refer to it like item.banos, item.dorms etc.

My first approach was something like

console.log(json.Object.key('301'));

Which didn't work

Any help would be very much appreciated.

6
  • 3
    console.log(json['301'])); Commented Sep 2, 2016 at 14:17
  • @jcubic anwer it, that's it Commented Sep 2, 2016 at 14:17
  • @KingRider please use English. Commented Sep 2, 2016 at 14:18
  • @jcubic that returns undefined Commented Sep 2, 2016 at 14:20
  • 1
    Possible duplicate of Access / process (nested) objects, arrays or JSON Commented Sep 2, 2016 at 14:28

3 Answers 3

3

It seems like your response is wrapped in an array with one element.


You can access object properties dynamically via square brackets:

var id = 301;
var url = "path/to/file.json";
$.getJSON(url, function(json) {
    console.log(json[0][id].banos);
});
Sign up to request clarification or add additional context in comments.

8 Comments

This did it, care to explain a bit on why do i have to use square brackets twice?
[0] accesses the first (and in this case only) element in the wrapping array. [id] accesses the property id of the object extracted from the array in the first step.
should i unwrap the array?
@TimoSta problem json is different object. Ur code example is correct.
@user2770956 That is kind of what [0] does. If you remove the array from the response on the server side, you don't have to do it in JavaScript -- if that is what you meant.
|
1

As you have the name of the property in the object you want to retrieve you can use bracket notation. you can also simplify your code because of this:

var id = 301;

//$.getJSON("path/to/file.json", function(json) {
  // response data from AJAX request:
  var json = {
    '301': {
      banos: 2
    },
    '302': {
      banos: 3
    },
    '303': {
      banos: 4
    },
    '304': {
      banos: 5
    },
  };

  var item = json[id];
  console.log(item);
//});

5 Comments

Thanks for your help, but this returns undefined. Also i didn't downvote.
I added a working example. If this code doesn't work, then you need to add your actual JSON to the question, not an image of the console representation of it.
@KingRider sorry, I have no idea what you're saying
@KingRider kinda difficult to understand you, bro
@KingRider I'm not sure if you are serious or not.
0
$.each(items,function(n,value){
    if(n==301)
        alert(value);
});

1 Comment

Please provide more details to your answer as this post has been found in low quality post. Code only and 'try this' answers are discouraged as it doesn't provide any searchable content and why people should 'try this'.

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.